Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8469335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:11:57+00:00 2026-06-10T16:11:57+00:00

I would like to upload multiple files using a single form field via Python

  • 0

I would like to upload multiple files using a single form field via Python CGI script using the code here. Newer browsers seem to support this feature per here and here. The HTML form seems straightforward:

<input name="file" type="file" multiple="" />

Using this form to select two files name file0 and file1 will result in the following per HTML5 input multiple attribute:

file=file0&file=file1

I initially assumed it would be an array of sort but it seems to use an ampersand for separation.

My attempt to modify the code and add a for statement to iterate through each file specified in the form field using the following code has been unsuccessful (see error below). I am up for other ideas that may also work using Python if using a for statement is not the best route.

#!/usr/bin/python
import cgi, os

form = cgi.FieldStorage()

# Generator to buffer file chunks
def fbuffer(f, chunk_size=10000):
   while True:
      chunk = f.read(chunk_size)
      if not chunk: break
      yield chunk

for fileitem in form['file']:

   # A nested FieldStorage instance holds the file
   fileitem = form['file']

   # 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)
      f = open('/var/www/domain.com/files' + fn, 'wb', 10000)

      # Read the file in chunks
      for chunk in fbuffer(fileitem.file):
         f.write(chunk)
      f.close()
      message = 'The file "' + fn + '" was uploaded successfully'

   else:
      message = 'No file was uploaded'

   print """\
   Content-Type: text/html\n
   <html><body>
   <p>%s</p>
   </body></html>
   """ % (message,)

Single file selected error:

 Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/cgi-bin/test.py", line 13, in <module>, referer: https://www.domain.com/files/upload.htm
     for fileitem in form['file']:, referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/python2.6/cgi.py", line 518, in __iter__, referer: https://www.domain.com/files/upload.htm
     return iter(self.keys()), referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/python2.6/cgi.py", line 583, in keys, referer: https://www.domain.com/files/upload.htm
     raise TypeError, "not indexable", referer: https://www.domain.com/files/upload.htm
 TypeError: not indexable, referer: https://www.domain.com/files/upload.htm
 Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm

Two files selected error:

 Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/cgi-bin/test.py", line 19, in <module>, referer: https://www.domain.com/files/upload.htm
     if fileitem.filename:, referer: https://www.domain.com/files/upload.htm
 AttributeError: 'list' object has no attribute 'filename', referer: https://www.domain.com/files/upload.htm
 Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm

If the .filename references are removed, a third error is produced, same for single or two files being selected:

Traceback (most recent call last):, referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/cgi-bin/test.py", line 24, in <module>, referer: https://www.domain.com/files/upload.htm
     fn = os.path.basename(fileitem), referer: https://www.domain.com/files/upload.htm
   File "/usr/lib/python2.6/posixpath.py", line 111, in basename, referer: https://www.domain.com/files/upload.htm
     i = p.rfind('/') + 1, referer: https://www.domain.com/files/upload.htm
 AttributeError: 'list' object has no attribute 'rfind', referer: https://www.domain.com/files/upload.htm
 Premature end of script headers: test.py, referer: https://www.domain.com/files/upload.htm
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:11:59+00:00Added an answer on June 10, 2026 at 4:11 pm

    Remove for file in form. The error implies that form['file'] is a list.

    Add to html form: method=post enctype=multipart/form-data.

    import shutil
    
    if 'file' in form:
       filefield = form['file']
       if not isinstance(filefield, list):
          filefield = [filefield]
    
       for fileitem in filefield:
           if fileitem.filename:
              fn = secure_filename(fileitem.filename)
              # save file
              with open('/var/www/domain.com/files/' + fn, 'wb') as f:
                  shutil.copyfileobj(fileitem.file, f)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to upload files via FTP to the sites/default/sites directory in Drupal
I would like to use this script to multiple file upload with jQuery v1.6.2.
Here is the code I'm using to upload Multiple Photos with the HTML5 tag.
I would like to upload files just like google mail does. I would want
I've just finished a functional piece of code and would like to upload it
I'm using CKEditor and would like to be able to allow users to upload
I'm currently using PHP to include multiple css (or js) files into a single
I have a script where users can upload multiple files (max. 8). The HTML
I have a seam app and would like to use the MultiPowUploader ( http://www.element-it.com/multiple-file-upload/flash-uploader.aspx
I'm creating a website and I'd like to allow users to upload multiple files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.