title: cgi python script seems not to receive uploaded file from html form
body: <p>I'm trying to set up a file upload web form that is processed by a python script. When I select a file and click upload, it says that no file was uploaded. The <code>file</code> field of the <code>fileitem</code> object is None. This script is running on a lighthttpd server.</p>
  
  <p>The code for the script is here:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import cgi, os
  import cgitb
  cgitb.enable()
  
  form = cgi.FieldStorage()
  # A nested FieldStorage instance holds the file
  fileitem = form['filename']
  print "----"
  print "filename", fileitem.filename
  print "file", fileitem.file
  print "----"
  
  message = ''
  if fileitem.file:
      # It's an uploaded file; count lines
      linecount = 0
      while 1:
          line = fileitem.file.readline()
          if not line: break
          linecount = linecount + 1
      message = linecount
  
  # Test if the file was uploaded
  if fileitem.filename:
  
     # strip leading path from file name to avoid directory traversal attacks
     fn = os.path.basename(fileitem.filename)
     open('/var/cache/lighttpd/uploads/' + fn, 'wb').write(fileitem.file.read())
     message = 'The file "' + fn + '" was uploaded successfully'
  
  else:
    message += 'No file was uploaded'
  
  print """\
  Content-Type: text/html\n
  &lt;html&gt;&lt;body&gt;
  &lt;p&gt;%s&lt;/p&gt;
  &lt;/body&gt;&lt;/html&gt;
  """ % (message,)
  </code></pre>
  
  <p>The html file is here:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;RepRap Pinter&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;H1&gt;RepRap Printer&lt;/H1&gt;
  &lt;form action="cgi-bin/start_print.py" method="post" encrypt="multipart/form-data"&gt;
  &lt;p&gt;&lt;input type="file" name="filename" id="file"&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input type="submit" value="Upload"&gt;&lt;/p&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And the output is:</p>
  
  <pre><code>----
  filename None
  file None
  ----
  Content-Type: text/html
  
  &lt;html&gt;&lt;body&gt;
  &lt;p&gt;No file was uploaded&lt;/p&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Any ideas as to why the file isn't being uploaded?</p>