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

Hi. I have an array with four jpg images and im having trouble printing/diplaying all of them out on a web page. Sometimes it displays one, two, three in random order and one pic is almost always repeated. Most of the time it displays one image, usually the last file in the array. i checked my uploads dir. and it only uploads one file even if i choose to upload 4. im on windows by the way ...
$upload_dir = 'C:/hut/uploads'; my $query->new CGI; my $filename = $query->param("photo"); # photo is the file name in +the html form my @fh = $query->upload("photo"); foreach $fhan(@fh){ open (UPLOADFILE, ">$upload_dir/$filename") or error($!); binmode UPLOADFILE; while ( <$fhan> ) { print UPLOADFILE; } } close UPLOADFILE; foreach $fhan(@fh) { print "<img src=\"uploads/$fhan\" alt=\"Pic\">\n"; }

Replies are listed 'Best First'.
Re: displaying image files in arrays
by dasgar (Priest) on Oct 05, 2010 at 22:40 UTC

    My first thought before looking at your code was to modify the code to write out (via print statements) the contents of the array to ensure that it contains what you think it contains. Still might not be a bad idea to do. Perl has an annoying habit of doing what you tell it do instead of what you wanted it to do. Adding print statements can help add clarity if you find yourself in that scenario.

    After looking at your code, I believe that you have some errors in in your first foreach loop. I've copied that below with modified indentation and added comments.

    my @fh = $query->upload("photo"); foreach $fhan(@fh){ open (UPLOADFILE, ">$upload_dir/$filename") or error($!); binmode UPLOADFILE; while ( <$fhan> ) { # Huh? $fhan is a variable, not the filehandle # Wait. You're printing what to where? # You should be using an img tag and providing # file path to the file in src or providing the binary # data in src print UPLOADFILE; } # end of while loop # Wait, we're approaching the end of the foreach loop # that starts with opening a file, but where's the code # to close the file? } # end of foreach loop # Need to move this close statement inside the foreach at # the end of each iteration close UPLOADFILE;

    After checking out the CGI module documentation, I believe that you might be going about things the wrong way. Checkout the section on Processing A File Upload Field.

    Conceptually, here's what you should be doing:

    1. Process the temp files to store them permanently where you want them.
    2. Generate the proper web based path for the permament paths created in previous steps
    3. For each path generated in the previous step, print the img tag that has the path in the src field's value.

    That's not what you're doing though. Before I try to post code to show you the "correct" way to do things, I would want to see the full source code of your script/HTML file that has the upload form and the the full source code of the script that processes it. Be sure to put those inside of <code></code> tags and may be even inside of <readmore></readmore> tags too. If you did post the entire source code for the processing script, then I'd say there's more problems that what I've pointed out.

      Thanks for the constructive criticism. i've taken your tips into consideration and revamping the whole thing. Anyway, the html code is ...
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html> <form action="http://localhost/code.pl" method="post" target="_blank" +enctype="multipart/form-data" > <input type="file" name="photo" size="15"> <input type="file" name="photo" size="15"> <input type="file" name="photo" size="15"> <input type="file" name="photo" size="15"> <input type="submit" value="Submit"><input type="reset" value="Start O +ver" /> </form> </html>
      i gave all file uploads the same name bcos its easier for me to put and arrange in an array initial perl code
      #!/usr/bin/perl use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser/; my $query = new CGI; $upload_dir = 'C:/mystuff_htm/uploads'; my @fh = $query->upload('photo'); my $filename = $query->param("photo"); print "Content-type: text/html\n\n"; foreach my $fhan(@fh){ open (UPLOADFILE, ">$upload_dir/$filename") or error($!); binmode UPLOADFILE; while ( <$fhan> ) { # i changed the $fhan and now working with @fh as filehandle print UPLOADFILE; } } close UPLOADFILE; foreach $fhan(@fh){ print "<img src=\"uploads/$fhan\" alt=\"Pic\">\n"; }