Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Upload images via browser ???

by vesakk (Initiate)
on Jul 05, 2000 at 09:34 UTC ( #21086=perlquestion: print w/replies, xml ) Need Help??

vesakk has asked for the wisdom of the Perl Monks concerning the following question:

Hi ! Is there very simple way to upload .gif or .jpg images via IE/NS browser to selected directory ??? ...if is, how can i do that ?

Replies are listed 'Best First'.
Re: Upload images via browser ???
by btrott (Parson) on Jul 05, 2000 at 10:13 UTC
    Take a look at the docs for the CGI module. It has functionality to allow file uploads through a form. You can upload any sort of file; in particular, you seem to be concerned with images. That works just fine.
Re: Upload images via browser ???
by toadi (Chaplain) on Jul 05, 2000 at 12:43 UTC
    Well,
    I did it a bit different then this : upload.pl but mine worked to.

    This is a good start...

    For the future:
    Before you start search foor cgi-scripts on a search engine and look at the documentation on CPAN and this is how you learn howto do new things.

    I know I do!

    --
    My opinions may have changed,
    but not the fact that I am right

RE: Upload images via browser ???
by tenatious (Beadle) on Jul 05, 2000 at 17:35 UTC

    Yes you can.

    The html that you use to create the form that you need to use can be created by CGI.pm, e.g.

    #!/usr/local/bin/perl -w # script name = upload_form.cgi use strict; use CGI; $query = new CGI; print $query->header; print $query->start_html; print $query->start_multipart_form(-action=>"image_writer.cgi"); #belo +w print $query->filefield(-name=>'file'); print $query->submit(-value=>'Upload File'); print $query->end_form; print $query->end_html; exit;

    Note that you have to use the start_multipart_form or else your upload will fail.

    The next bit comes straight out of the docs for CGI.pm. You might consider reading them and discover the difference between the upload and param methods.

    #!/usr/bin/local/perl -w # name image_writer.cgi use CGI; $query = new CGI; $file = $query->param('file'); open IMAGE, ">/tmp/image"; while ($bytesread = read($file,$buffer,1024) ) { #treat as binary $total += $bytesread; #add the bytesread to a total if ($bytesread > 2000000) { # no files larger than 2Meg, pleas +e close IMAGE; $badboy = $ENV{'REMOTE_ADDR'}; $time = scalar localtime; unlink ("/tmp/image"); #delete the file that's too large and l +og the offense (below) die "$time: $badboy tried to upload a file that was too large" +; } print IMAGE $buffer; } exit;

    That's about all there is to it. If you are using a Windows web-server, you'll have to modify where the file is saved, but it should work, otherwise. (there may be some minor problems with the code, but it's sound theoretically).

    I'd advise against using strict in the script that actually takes the upload, as there are some versions of CGI.pm that have problems with uploads with strict on (seems to be a problem with symbolic references or some other nonsense).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://21086]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-12-10 01:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?