Benny
04-23-2002, 11:25 AM
Hey all,
Was wondering how I can get cgi file uploading working using python (for uploading images to a webserver)...I thought I could do something like this:
photodir = "\images\photo"
def fileUpload():
form = cgi.FieldStorage()
if not form.has_key("file"):
print "content-type: text/html\n"
print """
<head>
<title>File Upload</title>
<body>
Upload your image</p>
<form action="stamps.py" method="POST" enctype="multipart/form-data">
<input type="hidden" name="sessionvalue" value="3">
Name of File: <input name="file" type="file" size="35">
<input name="submit" type="submit" value="Upload Image">
</form>
</body>
</html>
"""
else:
print "content-type: text/html\n"
if not os.path.exists(photodir):
os.mkdir(photodir,0777)
fn = form["file"].filename
if string.rfind(form["file"].filename,"\\") >= 0:
fn = fn[string.rfind(form["file"].filename,"\\"):]
if string.rfind(form["file"].filename,"/") >= 0:
fn = fn[string.rfind(form["file"].filename,"/"):]
if string.rfind(form["file"].filename,":") >= 0:
fn = fn[string.rfind(form["file"].filename,":"):]
o = open(photodir+os.sep+fn, "w")
o.write(form["file"].value)
o.close()
print "File uploaded"
But this method only seems to work for small text files, anything else and the file doesn't upload correctly (I want to use it for images only, so it's no good to me)....I saw the "cgiupload.py" module on the Python site, but I am unsure as to it's operation....?
Some feedback/advice would be much apprieciated.
Cheers.
Was wondering how I can get cgi file uploading working using python (for uploading images to a webserver)...I thought I could do something like this:
photodir = "\images\photo"
def fileUpload():
form = cgi.FieldStorage()
if not form.has_key("file"):
print "content-type: text/html\n"
print """
<head>
<title>File Upload</title>
<body>
Upload your image</p>
<form action="stamps.py" method="POST" enctype="multipart/form-data">
<input type="hidden" name="sessionvalue" value="3">
Name of File: <input name="file" type="file" size="35">
<input name="submit" type="submit" value="Upload Image">
</form>
</body>
</html>
"""
else:
print "content-type: text/html\n"
if not os.path.exists(photodir):
os.mkdir(photodir,0777)
fn = form["file"].filename
if string.rfind(form["file"].filename,"\\") >= 0:
fn = fn[string.rfind(form["file"].filename,"\\"):]
if string.rfind(form["file"].filename,"/") >= 0:
fn = fn[string.rfind(form["file"].filename,"/"):]
if string.rfind(form["file"].filename,":") >= 0:
fn = fn[string.rfind(form["file"].filename,":"):]
o = open(photodir+os.sep+fn, "w")
o.write(form["file"].value)
o.close()
print "File uploaded"
But this method only seems to work for small text files, anything else and the file doesn't upload correctly (I want to use it for images only, so it's no good to me)....I saw the "cgiupload.py" module on the Python site, but I am unsure as to it's operation....?
Some feedback/advice would be much apprieciated.
Cheers.