Re: Image Uploading
by Trimbach (Curate) on Oct 08, 2000 at 03:19 UTC
|
When you use CGI.pm for accessing a file uploaded through the browser the file upload field name is both the name of the uploaded file AND a CGI.pm-created filehandle. (You just gotta love that CGI module.)
So, if you want to spit the image back to the browser you can do something like what you've already done. On the other hand, if you want to save the file to the server for safe keeping you can:
# Copy a binary file to somewhere safe
open (OUTFILE,">>/usr/local/web/users/feedback");
while ($bytesread=read($filename,$buffer,1024)) {
print OUTFILE $buffer;
}
The above is straight from the CGI.pm documentation, where OUTFILE is the filehandle to where you want the uploaded image saved to.
Gary Blackburn
Trained Killer | [reply] [d/l] |
(ar0n) Re: Image Uploading
by ar0n (Priest) on Oct 08, 2000 at 02:13 UTC
|
read requires a filehandle, not a filename:
#!/usr/bin/perl -w
use strict;
my $file = "/etc/passwd";
my $data;
open(FILEHANDLE, "$file") || die "Can't read $file: $!\n";
print $data while(read(FILEHANDLE, $data, 1024));
close(FILEHANDLE);
[ar0n]
| [reply] [d/l] |
Re: Image Uploading
by cianoz (Friar) on Oct 08, 2000 at 17:06 UTC
|
first of all: remember to use start_multipart_form
and not just start_form() when you generate the input form,
as in
#!/usr/bin/perl -w
use CGI qw/:standard/;
print header();
print start_html();
print start_multipart_form(-action=>'test.pl');
print filefield('file_name'), submit();
print end_form;
print end_html;
then (test.pl)
#!/usr/bin/perl -w
use strict; ## ALLWAYS!!!!
use CGI;
use CGI::Carp qw(fatalsToBrowser);
$CGI::POST_MAX=1024 * 25;
my $q = new CGI;
my $file_name = $q->param('file_name');
my $fh = $q->upload('file_name'); # this is needed because use strict
if($fh) {
open OUT, ">/tmp/output.gif" || die "cannot open: $!";
binmode OUT; #just in case you are under windows
print $q->header('image/gif');
my $data;
while(read($fh,$data,1024)) {
print $data;
print OUT $data;
}
close OUT;
}
else {
print $q->header();
print $q->start_html();
print $q->h1('no upload file');
print $q->end_html();
}
| [reply] [d/l] [select] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 21:53 UTC
|
Yes!!!!!!!!!!!!!!!!!!!!!!!!
It worked!!
I took out the scrict and just worked it with the $file_name .
Now it's perfect!!!
Thanks a hell lot! You're a genius!!
If you want to check out my web site, which is in spanish cause it's for argentine teen you can do so at...
http://www.argenteen.com
In this site I use Perl, cause it's the best!
Thanks a lot!!
Ralph :) :) :)
| [reply] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 02:23 UTC
|
hi.
thanks for the reply.
there's a problem though, it doesn't create the image correctly. it comes out as an invalid image.
could it be that i have to do something special since i'm running it on windows 95?
ralph :) | [reply] |
|
|
Try calling binmode before you start printing:
binmode FILEHANDLE;
print $data while(read(FILEHANDLE, $data, 1024));
[ar0n]
| [reply] [d/l] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 18:53 UTC
|
hi.
thanks for the reply.
i tried doing exactly as told above, but when i run it my browser pops up a message saying...
the document contained no data.
try again later, blablabla..
what could the problem be?
ralph :) | [reply] |
|
|
have a look at your server error log
the script i posted works fine for me...
| [reply] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 19:23 UTC
|
hi.
thanks fo the reply.
i've looked at the error log from the server, there are no errors there though.
could it be some module problem? any ideas? i'm running savant web server on a windows 95 machine.
ralph :) (i hate windows...) | [reply] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 20:18 UTC
|
hi.
now i've tried it on another computer, with another web server prog (xitami) and this time it executes, but it gives me another error message though...
Undefined subroutine CGI::upload
Does anyone know what this means and if it can be fixed?
Ralph :) | [reply] |
|
|
you have to upgrade CGI.pm,
(older versions lack upload() function)
the other choice you have is to use
$file_name as file handle prepending "no strict;"
see "perldoc CGI" for more details.
| [reply] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 02:39 UTC
|
hi.
i've tried the binmode, and it still doesn't work with that.
if it's not much trouble...
could u please write a full script for an image coming from a form with field name="file"
thanks
ralph :) | [reply] |
|
|
| [reply] |
Re: Image Uploading
by Anonymous Monk on Oct 08, 2000 at 04:39 UTC
|
hi.
it's me ralph.
i've tried all the options above, but i can't make any of them work.
could anyone write a super simple script that will upload a gif image to a folder in my hard drive?
(considering the name of the file field in the form being "file_name")
ralph :) | [reply] |