How do I detect that a particular form element is a file upload or if the file upload has worked?
In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage() fileitem = form["userfile"] if fileitem.file: # It's an uploaded file; count lines I've tried something like this in a test script and fileitem.file evaluates to true when form element "userfile" is just a text box in the web page. When "userfile" is a type="file" it evaluates true when I select a real file to upload and when I manually type in a false path to a file that doesn't exist. When I do upload a file, the script correctly prints out the file contents (fileitem.value) so the upload does work OK. It seems that provided there's a form element called "userfile" then fileitem.file evaluates to true provided the form has enctype="multipart/form-data" (it doesn't matter if the file upload works or if the form element is not type="file"). When I tried different forms without the enctype then fileitem.file evaluated to false when userfile was either type="text" or type="file". My conclusion is that if fileitem.file: detects enctype of form rather than if a particular element is a file upload. How do I detect that a particular form element is a file upload or if the file upload has worked? My best suggestion (but I'd like to hear your views) is to use: if fileitem.filename and len(fileitem.value): # It's an uploaded file When the form element userfile is just a text box or the form lacks the enctype then the filename attribute evaluates to false. When I supply an incorrect file path to the file form element the upload fails so len(fileitem.value) evaluates to false. When I upload a real file, both fileitem.filename and len(fileitem.value) evaluate to true. If I upload a real file that's empty then fileitem.value is an empty string so len(fileitem.value) evaluates to false which is not quite right (an empty file is an obscure test but it is a valid file to upload). Any suggestions for improvements would be gratefully received. Thanks Mark PS if it's relevant, I'm using Windows2000, Python 2.3.2, the shebang includes -u, Apache 2.0.42 and Firefox 1.0.4. -- http://mail.python.org/mailman/listinfo/python-list