Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

upload image with cgi

by weihe (Initiate)
on Sep 27, 2006 at 05:02 UTC ( [id://575082]=perlquestion: print w/replies, xml ) Need Help??

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

i know how to upload file,but i don't know upload image.
upload file example
#!/usr/local/bin/perl use CGI; $co = new CGI; if (!$co->param()) { print $co->header, $co->start_html('CGI File Upload Example'), $co->center ( $co->br, $co->center($co->h1('CGI File Upaload Example')), $co->start_multipart_form, $co->filefield(-name=>'filename', -size=>30), $co->br, $co->submit(-value=>'upload'), $co->reset, $co->end_form ), $co->hr; }else{ print $co->header, $co->start_html('CGI File Upload Example'), $co->center($co->h1('CGI File Upload Example')); $file = $co->param('filename'); @data = <$file>; foreach (@data){ s/\n/<br>/g; } print $co->center($co->h2("Here's the contents of $file...")), "@data"; } print $co->end_html;

Replies are listed 'Best First'.
Re: upload image with cgi
by grep (Monsignor) on Sep 27, 2006 at 05:46 UTC
    What do you need to know? You example works uploading a file. You just need to write it to a file. So instead of doing a print to the browser (which would just show the data of the image not the actual image) just write it to a file.

    Just check to make sure it's an image and write it to a binmode'ed file handle.

    Untested Code:

    my $filename = $cgi->param('uploaded_file'); my $type = $cgi->uploadInfo($filename)->{'Content-Type'}; #use the upload function if ($type eq 'image/jpeg') { open(IMAGE,">/image_dir/filename.jpg"); my $fh = $cgi->upload('uploaded_file'); binmode(IMAGE); while (<$fh>) { print IMAGE $_; } print '<img src="/image_dir/filename.jpg">'; } else { print 'Not a JPEG<br>'; }


    grep
    Mynd you, mønk bites Kan be pretti nasti...
Re: upload image with cgi
by Praveen (Friar) on Sep 27, 2006 at 05:41 UTC
    Try This.
    #!/usr/bin/perl -w use CGI; $upload_dir = "/home/mywebsite/htdocs/upload"; $query = new CGI; $filename = $query->param("photo"); $filename =~ s/.*[\/\\](.*)/$1/; $upload_filehandle = $query->upload("photo"); open UPLOADFILE, ">$upload_dir/$filename"; binmode UPLOADFILE; while ( <$upload_filehandle> ) { print UPLOADFILE; } close UPLOADFILE;
      This worked for me. Thank you.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-25 21:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found