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

Greetings Wise Ones

I am trying to write an image gallery but it's not quite working for me. I'm following the CGI.pm manpage. At the moment, when I run the script through the webpage I get a weird error.

First the code:

#!/usr/bin/perl use strict; use warnings; use CGI qw /normal/; use File::Basename; my $query = CGI->new(); my $bytesread; my $buffer; my $filename; print $query->header; print $query->start_html; print $query->start_form; print $query->br; print "Enter picture description"; print $query->br; print $query->textfield(-name=>'name', -default=>'description', -size=>50, -maxlength=>250); print $query->br; print "Enter filename to upload"; print $query->br; print $query->filefield(-name=>'uploaded_file', -default=>'starting value', -size=>50, -maxlength=>80); print $query->submit; $filename = $query->upload('uploaded_file'); while (<$filename>) { print "$_<br>"; } open (OUTFILE,">/tmp/feedback"); while ($bytesread=read($filename,$buffer,1024)) { print OUTFILE $buffer; } print $query->end_html;
And now the error:

[Fri Aug 13 23:08:33 2004] [error] [client 192.168.1.3] Use of uniniti +alized value in <HANDLE> at /var/www/cgi-bin/gallery/upload.cgi line +37., referer: http://www.retard.com/cgi-bin/gallery/upload.cgi [Fri Aug 13 23:08:33 2004] [error] [client 192.168.1.3] readline() on +unopened filehandle at /var/www/cgi-bin/gallery/upload.cgi line 37., +referer: http://www.retard.com/cgi-bin/gallery/upload.cgi [Fri Aug 13 23:08:33 2004] [error] [client 192.168.1.3] Can't use an u +ndefined value as a symbol reference at /var/www/cgi-bin/gallery/uplo +ad.cgi line 42., referer: http://www.retard.com/cgi-bin/gallery/uploa +d.cgi
I'm confused as the manpage says that $filename = $query->upload('uploaded_file');, $filename is also a file handle.

Can someone give me a nudge in the right direction?

Thanks

ReTard (not the quickest function in the library)

Replies are listed 'Best First'.
Re: Problem with file upload CGI script
by davorg (Chancellor) on Aug 13, 2004 at 15:22 UTC

    Read the bit in the docs about start_multipart_form.

    Also, you might need to "binmode" your filehandles - it's good practice to do it anyway to keep your code cross-platform.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Problem with file upload CGI script
by eric256 (Parson) on Aug 13, 2004 at 15:23 UTC

    Your problem is not with the file handling. It is that you are trying to read the uploaded file at the same time as you are printing the form. Your script is being called to create the form, when the user then submits something it will be resubmited to that same script. So you need to have a way to know if its the initial request, in which case you print the form, or if they have already filled it out, in which case you upload the file.


    ___________
    Eric Hodges
Re: Problem with file upload CGI script
by nikos (Scribe) on Aug 13, 2004 at 17:01 UTC
    Hi

    1) you should do something like
    my $query=new CGI(); my $filename=$query->upload("uploaded_file"); if( defined($filename) ) { # save the uploaded file open(OUTPUT, ">1.jpg"); # just an example, you should check the type binmode($filename); binmode(OUTPUT); while( read($filename, $buffer, 64*2**10) ) { # filename is a file ha +ndle here print OUTPUT $buffer; } close(OUTPUT); print $query->header(); print $filename; # it's a string here; original filename returned (th +e name on client's harddrive, fullpath/filename } else { # print the form print $query->header(); ... print $query->end_html(); }
    This code was written by memory, not tested. I wrote a similar script just a few days ago. Please feel free to correct me if I'm wrong. 2) CGI.pm returns a string with an original full path/filename on client's computer when $filename is used as a string. If it's used as a file handle, a file handle is returned 3) you can get the MIME type of the uploaded file from CGI.pm. but if you want to be sure 100%, let's say, .gif is a gif actually, .jpg is a JPEG, you should check the file's content using 'file' utility thru a system call or an image-processing library like PerlMagick (ImageMagick). But it goes far away from the original question
Re: Problem with file upload CGI script
by tinita (Parson) on Aug 14, 2004 at 12:50 UTC
    just another thing i spotted:
    while (<$filename>) { ... } while ($bytesread=read($filename,$buffer,1024)) {
    after the first while $filename will be at eof and return undef.