Re: Uploading images via cgi?



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/Cookbook/Python/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?
.



Relevant Pages

  • Re: Need to add something to form to make file uploads possible.. but what?
    ... J> I found a really useful tutorial for cgi beginners ... J> script and when I tested the online form it did indeed get emailed ... J> I found some html code somewhere on the web to create the buttons ... to send email and not do upload (at least according to what i read. ...
    (comp.lang.perl.misc)
  • Re: Key-passing from PHP to TCL CGI script - how is it done (web security issue)?
    ... TCL v.8.3 ... set hasEnteredTrivia 0 ... # NEW 8/7/2004 USE PHP (AS CGI) TO CHECK IF USER IS ATTEMPTING TO ENTER ... # USE REMOTE PHP SCRIPT TO CHECK FOR REPOSTING ...
    (comp.lang.tcl)
  • Re: Run CGI in php
    ... Due to a limitation of PHP from my host I need to run a ... cgi script to upload a file larger than 2MB (PSUpload ... This script work good but i need to fill others information about the ... did anyone know how to execute this CGI perl script in php so i ...
    (comp.lang.php)
  • Re: Changing Passwords through the web (fwd)
    ... I use a CGI script that uses the poppassd and python ports on my small ... alert; ...
    (FreeBSD-Security)
  • Re: Help with a stragegy for diagnosis
    ... I have a Perl CGI that has work for years. ... first failure and before the second. ... A strategy would be to write tests for the script until you get the same ...
    (comp.lang.perl.moderated)