CGI File Upload
Simon Faulkner
news at titanic.co.uk
Tue Nov 7 15:07:45 EST 2000
Yeah!
It works... Thanks
Now, how do I pipe this file through Image MAgik to resize it?
Simon
On Mon, 6 Nov 2000 15:44:24 -0800, "Pete Shinners"
<pete at visionart.com> wrote:
>"Simon Faulkner" <news at titanic.co.uk> wrote
>> I would like users to be able to upload .jpeg files via a web page
>> <input type=file>
>>
>> What python code do I need to be able to save the file to a directory
>> on the hard disk?
>
>i have some code that does this.. you'll probably want to clean
>it to suit your own purposes, but here it is. at some point i
>wanted to add PIL support to resize the images to a certain
>resolution and convert them to be JPG, but that day hasn't come
>yet... heh. note it also allows the user to save a comment as a
>.TXT file. anyways, you can use it as a start for whatever you want
>
>(hopefully formatting doesn't get destroyed over news...)
>(warning, this is one of my very first python scripts, it
>may not be 100% kosher, but it has been working well)
>
>two files here, one is most of my cgi script, other is the
>page with the form that calls the script...
>
>-----------------------------------------------------------------
>
>import sys,os,cgi,glob,string,time
>import members #my membership user list
>import header #cgi script to build my page header
>sys.stderr = sys.stdout
>
>
># directory for upload; will be created if doesn't exist
>dirUpload = "/home/ACCOUNT/public_html/photos"
>
># where to email upload reports
>email = "ACCOUNT at server.com"
># sendmail will email notification of uploads
>sendmail = "/usr/sbin/sendmail"
>
>
>
>
>def mailme(msg=""):
> "Quick and dirty, pipe a message to sendmail"
> if email:
> try:
> o = os.popen("%s -t" % sendmail,"w")
> o.write("To: %s\n" % email)
> o.write("From: %s\n" % email)
> o.write("Subject: %s\n" % "Photo Upload")
> o.write("\n")
> o.write("%s\n" % msg)
> o.write("---------------------------------------\n")
> for x in [
>'REQUEST_URI','HTTP_USER_AGENT','REMOTE_ADDR','HTTP_FROM'
>,'REMOTE_HOST','REMOTE_PORT','SERVER_SOFTWARE','HTTP_REFERER','REMOTE_IDENT'
>,'RE
>MOTE_USER','QUERY_STRING','DATE_LOCAL' ]:
> if os.environ.has_key(x):
> o.write("%s: %s\n" % (x, os.environ[x]))
> o.write("---------------------------------------\n")
> o.close()
> except IOError:
> pass
>
>
>def handleform():
> data = cgi.FieldStorage()
> try:
> members = members.MemberList()
> m = members[data['name'].value]
> account= m['account']
> password = data['pass'].value
> if members.checkpassword(account, password) == 0:
> raise IOError, 'Invalid Password'
> except:
> badinput('Invalid user account or password. Please try again.',
>data)
> return
>
> if not data.has_key('image'):
> return
>
> try:
> filename = os.path.join(dirUpload, m['name']+'.jpg')
> o = open(filename, 'wb')
> o.write(data['image'].value)
> o.close()
>
> filename = os.path.join(dirUpload, m['name']+'.txt')
> o = open(filename, 'w')
> o.write(data['comment'].value)
> o.close()
> except:
> badinput('No Image Uploaded', data)
>
> header.header('Upload Successful', 'account')
> print "<br><font size=+1>Image Uploaded Successfully.</font><br>"
> print "You may return to the <a href=/photos.shtml>Photos</a> page "
> print "to view your new addition.<br>"
> print "You will probably need to <u>Reload</u> the Photos Page once "
> print "you get there."
> print "</body></html>"
>
> mailme('New Image Uploaded by '+m['account'])
>
>
>
>if __name__ == '__main__':
> print "content-type: text/html\n\n<html>"
> handleform()
>
>
>---------------------------------------------------------------
>
><html>
><!--#exec cmd="cgi-bin/header.py UploadPhotos photos" -->
>
><form method=post action=/cgi-bin/newphoto.py enctype="multipart/form-data">
><table cellpadding=0 cellspacing=0><tr><td align=right>
>Account: </td><td><input type=input name=name value=""
>size=13></td></td><tr><td align=right>
>Password: </td><td><input type=password name=pass value=""
>size=13></td></td><tr><td align=right>
>Comment: </td><td><input type=input name=comment value=""
>size=40></td></td><tr><td align=right>
>Image: </td><td><input type=file name=image size=40>
></td></tr><tr><td></td><td><input type=submit value="Upload This Picture">
></td></tr></table></form>
>
></body></html>
>
>
More information about the Python-list
mailing list