#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    1
    Rep Power
    0

    cgi scripting file upload ?


    hey..woul anyone happen to have a python cgi script that will allow file uploading..i want to allow the user to upload photos onto my websit ...any help will be appreciated..thanx
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    There is an example in the ASPN Python cookbook:

    http://aspn.activestate.com/ASPN/Coo.../Recipe/273844

    The important point is that the HTML <form> tag must have the attribute enctype="multipart/form-data". Then you can get the file data from the FieldStorage.file attribute.

    Dave - The Developers' Coach
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Heres a very basic upload script staight form my head that should work, although untested. If you want to accept image uploads then you will need to write the file out using the wb flag but the basics are there. You might also want to check that the file doesnt already exist, this way you dont loose anything from a file conflict.

    Code:
    #!/usr/bin/env python
    
    import cgi, os
    
    form = cgi.FieldStorage()
    
    if 'upload' in form:
        name =  os.path.split(form['upload'].filename)
        file(name[-1], 'w').write(form['upload'].value)
    Mark.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo