Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Problems with 'image upload'

by JP Sama (Hermit)
on Nov 07, 2000 at 16:22 UTC ( #40331=perlquestion: print w/replies, xml ) Need Help??

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

Dear PerlMonks,

I'm having troubles with a CGI to upload images to my site, I've tried lots of ways to solve it... but none worked!
the code i'm using is something like that:
#!/usr/bin/perl use CGI; use CGI::Carp 'fatalsToBrowser'; CGI::ReadParse(*in); my $homeurl = ''; my $imagedir = '/www/joao/images'; my ($s,$m,$h,$mday,$mon,$year,$w,$y) = gmtime(time); my $id = $year.$mon.$mday; # save thumbnail my $thumb = &FileUpload($in{iddt}); open(MAKETHUMB,">$imagedir/$idt.gif") or die("Could not save $idt.gif"); print MAKETHUMB $thumb; close(MAKETHUMB); # save image 'idd' my $img = &FileUpload($in{idd}); open(MAKE,">$imagedir/$id.gif") or die("Could not save $id.gif"); print MAKE $img; close(MAKE); # the images are mine! my @files = glob("$id*.gif"); chown 'joao','fodas',@files; # upload file (used here to upload images) sub FileUpload { my ($formfield) = shift; my ($filename,$filesize,$filedata); # load query handle $query = $in{CGI}; if ($filename = $query->param($formfield)) { my $tmp = $/; undef($/); $filedata = <$filename>; $filesize = length($filedata); $/ = $tmp; } # find what the trails look like and get the # true file name without the garbage if ($filename =~ m/[\\\/\:]/gi) { $filename =~ s/[\\\/\:]/\//g; my @tmp = split(/\//,$filename); $filename = pop(@tmp); } return($filedata); }
well, with this i've got the following:
  1. the thumbnail file is not created;
  2. The second image is created but nothing is printed, the image remains with 0k, and is owned by wwwrun;
Because of that, i ask:
why were my images not uploaded?
and why my 'chown' is not working properly?

Can anyone help me? please!
Thank you!!

PS: Oh, and i'm using 'enctype="multipart/formdata"' in my form... and the fields are with the correct names...

#!/jpsama/bin/perl -w
$tks = `mount`;
$jpsama = $! if $!;
print $jpsama;

Replies are listed 'Best First'.
Re: Problems with 'image upload'
by Fastolfe (Vicar) on Nov 07, 2000 at 18:45 UTC
    Please read the CGI documentation, specifically, the section devoted to file upload processing. It has examples. Basically you're doing this wrong:
    my $img = &FileUpload($in{idd}); open(MAKE,">$imagedir/$id.gif") or die("Could not save $id.gif"); print MAKE $img; close(MAKE);
    Should be something like this:
    my $img = &FileUpload($in{idd}); open(MAKE,">$imagedir/$id.gif") or die("Could not save $id.gif"); binmode MAKE; print MAKE while <$img>; close(MAKE);
Re: Problems with 'image upload'
by Trimbach (Curate) on Nov 07, 2000 at 17:53 UTC
    Your main problem is that you should use the file upload fieldname as a filehandle directly instead of trying to copy it into another variable. Don't try $filename =    $query->param($formfield) but rather $input = <param($formfield)> The documention on goes into this in some detail.

    Better yet, check out the copious examples of file uploading code found elsewhere in the site, such as (Ovid) Re: File Upload To Selected Directory (it doesn't matter that the example doesn't happen to be an image.)

    Gary Blackburn
    Trained Killer

Re: Problems with 'image upload'
by kilinrax (Deacon) on Nov 07, 2000 at 17:10 UTC
    The UID and GID passed to chown should be numerical.

    (Try 'echo $UID' at a shell prompt if you don't know what your numeric user id is.)
Re: Problems with 'image upload'
by cianoz (Friar) on Nov 07, 2000 at 19:34 UTC
    remember to put <FORM METHOD="POST"  ENCTYPE="multipart/form-data"> on the form where you place the file upload field
      geez! Thank you! After "years" trying... I finally made it...!
      thanks to your hint..

      My f@#$@% FORM was:
      <FORM METHOD="POST" ENCTYPE="multipart/formdata">

      note the formdata part!

      Well... Thank you cianoz!!
      And thank to you all!
      #!/jpsama/bin/perl -w
      $tks = `mount`;
      $jpsama = $! if $!;
      print $jpsama;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2022-08-10 04:53 GMT
Find Nodes?
    Voting Booth?

    No recent polls found