CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:
Uploading images


New User

May 23, 2004, 4:25 PM

Post #1 of 3 (693 views)
Uploading images Can't Post

I'm attempting to upload images for use with a retail application, using input from a form. I'm able to get the file to upload to the target file but something happens that renders it unusuable. It appears to be the same size as the original, but it won't resolve when called by a browser. I'm guessing that perl is parsing the file at some point between the time it's loaded into the buffer and the time it's written.

I wrote the code using my normal techniques, identifying the image as a file instead of text on the form. Unable to get it to work properly, I tried rewriting the routine to use CGI:pm but being totally unfamilair with that, I couldn't get it to work at all. I'm now back to my original script. Unsure

If anyone can shed some light on my problem, I'll be grateful. I'm running on a UNIX system but I added a binmode line as a last resort. It didn't change the results. If it turns out that I must use CGI:pm I'll rewrite the routine again. Here's what I'm using right now:

$pr_medphoto = $INPT{'pr_medphoto'};

if ($pr_smlphoto ne "") {

$pr_skut = $pr_sku . t;

&upload_image("$pr_smlphoto","$photo_dir/$pr_skut.jpg"); }


sub upload_image # Upload parameters are (field name, output dir/file)



binmode OUT;

print OUT $_[0];

close OUT;



Thanks for your help.

Thaumaturge / Moderator

May 24, 2004, 7:50 AM

Post #2 of 3 (683 views)
Re: [TJD] Uploading images [In reply to] Can't Post

Have you tried looking at the file that your subroutine writes? Does it look like binary data? I think you might just be writing the name of a filehandle or something like that to the file. Don't you have to read the data in at some point?

To be honest, I'm not 100% sure as I find it far easier to always use to upload files.

Oh, also... does your form have the multi-part stuff?

Dave Cross, Perl Hacker, Trainer and Writer
Get more help at Perl Monks

New User

May 24, 2004, 2:11 PM

Post #3 of 3 (679 views)
Re: [davorg] Uploading images [In reply to] Can't Post

In reviewing my original post I realized that I made a small mistake while performing the cut-and-paste (plus at least one spelling error). The line :

$pr_medphoto = $INPT{'pr_medphoto'};

should be :

$pr_smlphoto = $INPT{'pr_smlphoto'};

I'm attempting to upload several photos of varying sizes from the form and I grabbed the wrong line for the post.

***** new business

I spent the weekend thinking about this problem and little else, especially once I submitted the post. As I tried to fall asleep last night, my mind keep dragging me back to the routine I use to read data input.

Like most programmers, I taught myself to do things a certain way and continue to employ those same thechniques until I run into a problem that sends me back to the reference books to find new procedures. The input routine that I used is one that I've been using since I started writing in Perl a few years ago. I simply copy it into every script I write so I'm not reinventing the wheel each time. I'm not even sure where it came from. I probably either got it from a book or from a sample script somewhere.

After I read your reply today I pulled the input routine apart and examined every line. After staring at it for what seemed like hours, I found my error. In the parameters that I was sending to the subroutine, I sent '$smlphoto' as the handle. I should have been sending 'smlphoto'. Secondly, I should have been using :

print OUT $INPT2{$_[0]};

in the upload subroutine instead of :

print OUT $_[0];

The $INPT2 file is loaded in the input routine before a couple of substitution operators are used, thereby modifying the input.

Thanks for your help. The suggestions got me looking at the routines in a new light. Sometimes you can't see the forest for the trees.Smile

In my next new script I'm going to again try to use CGI:pm. It's about time I learned it.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives