By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Uploading images via cgi?

P: n/a
How do I go about writing a cgi script, that will enable the client to
upload things to a directory on my website?

I would also like to write a script that enables the client to delete
items in that directory if they want. Given that it's unix server, how do
I go about ensuring the files are written to that dir with the right
permissions?
Aug 31 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chris Dewin wrote:
How do I go about writing a cgi script, that will enable the client to
upload things to a directory on my website?

I would also like to write a script that enables the client to delete
items in that directory if they want. Given that it's unix server, how do
I go about ensuring the files are written to that dir with the right
permissions?


What did you learn when you tried a few of the more obvious searches
with Google (including the word "python" in the search)? You must have
learned _something_ but still have questions, so please tell us what you
already know so we can skip over the easy stuff.

Knowing what you've already discovered will avoid having everyone here
repeat all the same answers (which you can easily find in the Google
Groups archives) to what is a fairly common sort of question.

-Peter
Aug 31 '05 #2

P: n/a
On Tue, 30 Aug 2005 21:55:26 +0000, Peter Hansen wrote:
Chris Dewin wrote:
How do I go about writing a cgi script, that will enable the client to
upload things to a directory on my website?

I would also like to write a script that enables the client to delete
items in that directory if they want. Given that it's unix server, how
do I go about ensuring the files are written to that dir with the right
permissions?
What did you learn when you tried a few of the more obvious searches
with Google (including the word "python" in the search)? You must have
learned _something_ but still have questions, so please tell us what you
already know so we can skip over the easy stuff.

Knowing what you've already discovered will avoid having everyone here
repeat all the same answers (which you can easily find in the Google
Groups archives) to what is a fairly common sort of question.

-Peter


Very well then. I did search google, and found a number of python scripts
that already do the job (more or less). But when I try to tweak them to my
specifications, I get errors ... probably because I don't really
understand how the process of uploading a file via a webform to a cgi
works.

For e.g, take the script at this url.

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

By reading through this script, I know that simply putting '<input
name="file_1" type="file">' in your web form will establish a connection
to the cgi when the client submits the form. The python interpreter
obviously sustains that connection while the file is being transferred,
the file being part of the FieldStorage() instance. But then, the script
saves the file to the disk in some special way I don't understand. In this
case:
fout = file (os.path.join(upload_dir, fileitem.filename), 'wb') while
1:
chunk = fileitem.file.read(100000)
if not chunk: break
fout.write (chunk)
fout.close()


chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?
Aug 31 '05 #3

P: n/a
On Wed, 31 Aug 2005 13:28:35 +0900, "Chris Dewin" <ch***@wintergreen.in>
declaimed the following in comp.lang.python:
fout = file (os.path.join(upload_dir, fileitem.filename), 'wb') while
I'm presuming the "while" was really on the next line with the 1:
1:
chunk = fileitem.file.read(100000)
if not chunk: break
fout.write (chunk)
fout.close()
chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?


The read is attempting to read, as the variable says, "chunks" of
the file up to a maximum of 100K(decimal) bytes at a time, then writes
that "chunk" to the local output file. The loop is needed to retrieve
the rest of the file data. When the read returns an empty "chunk", the
file is done..
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Aug 31 '05 #4

P: n/a
Chris Dewin wrote:
chunk = fileitem.file.read(100000), and fout.write (chunk) seem to be the
key statements. Could someone elaborate on these for me please? And why
do they need to be in a while loop?


I imagine that the intention of reading only 100000 bytes at a time is
to limit the amount of memory used by the process. In an environment
where the process is kept around (not CGI, however) doing so might be
critical since it has been alleged that Python isn't particularly good
at freeing memory back to the operating system.

One way of limiting the total size of uploaded files might be to
examine the "Content-Length" header and to reject (ie. not process)
uploads involving files above a certain size.

Paul

Aug 31 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.