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

Hi.

I'm having a problem with some image uploading code. It's driving me crazy, cause on one script it works fine while on this new one, it doesn't work :(

Here's the code...

for($i = 1; $i <= 11; $i++) { $pcaption = $fr{"caption$i"}; $paction = $fr{"action$i"}; $photo = $fr{"photo$i"}; if($paction eq 'delete') { unlink("../../listing/$ident-$fr{listingi +d}-$i.jpg"); } if($photo ne '') { $photodir = ">../../listing/$ident-$fr{listingid}-$i.jpg"; $fh = $q->upload("photo$i"); open(PFILE, $photodir); while(<$fh>) { print PFILE; } close(PFILE); } $sth = $dbh->prepare("insert into listing_photo values ( '$ident', $ +fr{listingid}, $i, '$pcaption' )"); $sth->execute; $sth->finish; }


Now, when I upload an image, it does create the file in the 'listing' folder but it doesn't upload any data. It creates a blank image file :( Why?? I can't seem to find where the problem is.

Please help.

Thanks,
Ralph.

2002-05-06 Edit by Corion : Added CODE tags

Replies are listed 'Best First'.
Re: Problem with Image Uploads
by Anonymous Monk on May 07, 2002 at 08:20 UTC
    Damn, i'm an idiot! :p

    The problem was that I had forgotten the enctype="multipart/form-data" in the <form> tag.

    rjray, thanks for waking me up :)

    Ralph :p
Re: Problem with Image Uploads
by cLive ;-) (Prior) on May 07, 2002 at 00:06 UTC
    From a quick scan, you might want to open the file for writing :)
    open(PFILE, ">$photodir") || die "Cannot open image file for writing - + $!";

    .02

    cLive ;-)

Re: Problem with Image Uploads
by rjray (Chaplain) on May 07, 2002 at 06:21 UTC

    Are you certain that the application is correctly presenting the form to begin with, and that valid data is being transmitted? Your sample code above is not checking the return value from the upload method to see if it is a valid value. It may be undef.

    --rjray

Re: Problem with Image Uploads
by Cody Pendant (Prior) on May 07, 2002 at 02:18 UTC
    If the file is created, then it means we've got this far:
    if($photo ne '') { $photodir = &quot;&gt;../../listing/$ident-$fr{listingid}-$i.jpg&q +uot;; $fh = $q-&gt;upload(&quot;photo$i&quot;); open(PFILE, $photodir); while(&lt;$fh&gt;) { print PFILE; } close(PFILE); }

    In fact we've got as far as opening the file and that's all worked OK.

    This thing is so stuffed with variables that only you understand, I think what I'd do is have all my variables printed out on screen so I could check they were doing what I thought they were doing, like, I'd put:

    print "I'm now trying to open /listing/$ident-$fr{listingid}-$i.jpg"

    at each step, just so I'd get a confirmation of what was being called.

    Clunky but it works for me.
    --

    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
Re: Problem with Image Uploads
by Anonymous Monk on May 07, 2002 at 00:52 UTC
    Nope, it still doesn't work. It just creates an empty file :(

    Ralph.