Uploading images via cgi?
chris at wintergreen.in
Wed Aug 31 00:28:35 EDT 2005
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
> 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.
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
For e.g, take the script at this url.
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
> fout = file (os.path.join(upload_dir, fileitem.filename), 'wb') while
> chunk = fileitem.file.read(100000)
> if not chunk: break
> fout.write (chunk)
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?
More information about the Python-list