in reply to Re: Re: Yet another CGI Upload problem - not like the others!
in thread Yet another CGI Upload problem - not like the others!

To debug this change the die to  die $query->cgi_error()

If that is not revealing then have a look at the CGI object which should contain a blessed? reference to the upload file's tmpdir name.

use CGI; use Data::Dumper; $q = new CGI; print $q->header; print '<pre>' . $q->escapeHTML(Data::Dumper::Dumper($q)) . '</pre>';

This will prove one way or another if the file is getting to the server and being written to a tmp dir. This may well be the problem. When you do the upload the file is spooled by CGI.pm to a tmp dir ( CGI.pm uses /tmp /temp and something else in that order from memory). If it can't write here it can't spool. Then when you call upload() it will have nothing to deliver and return false, thus causing death as described.

If that fails then try my CGI::Simple module which has the same interface as CGI.pm but far more inclusive and revealing cgi_error() messages. It uses IO::File's tmpfile() method to get a FH for spooling so does not need write permission to /tmp. It also has an addition to the upload() method that lets you do the upload in one line. See the docs for details.

use CGI::Simple; my $q = new CGI::Simple; # <INPUT TYPE="file" NAME="upload_file" SIZE="42"> $files = $q->upload() # number of files uploaded @files = $q->upload(); # names of all uploaded fi +les $filename = $q->param('upload_file') # filename of uploaded fil +e $mime = $q->upload_info($filename,'mime'); # MIME type of uplo +aded file $size = $q->upload_info($filename,'size'); # size of uploaded +file my $fh = $q->upload($filename); # get filehandle to read f +rom while ( read( $fh, $buffer, 1024 ) ) { ... } # short and sweet upload $ok = $q->upload( $q->param('upload_file'), '/path/to/write/file.n +ame' ); print "Uploaded ".$q->param('upload_file')." and wrote it OK!" if +$ok;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: Re: Re: Yet another CGI Upload problem - not like the others!
by zentara (Cardinal) on Dec 11, 2002 at 19:17 UTC
    Hi, I thought I would try out your simple method of file upload. It works fine except the maximum file size dosn't seem to work. I'm uploading 2 meg files to the cgi script below, why isn't the limit working?
    #!/usr/bin/perl use warnings; use strict; use CGI::Simple; $CGI::Simple::POST_MAX = 1024; # max upload via post default 100 +kB $CGI::Simple::DISABLE_UPLOADS = 0; #enable uploads my $upload_dir='uploads'; my $q = new CGI::Simple; print $q->header(); my $files = $q->upload(); # number of files uploaded +; my @files = $q->upload(); # names of all uploaded fil +es my $filename = $q->param('upload_file'); # filename of uploaded fil +e my $mime = $q->upload_info($filename,'mime'); # MIME type of uploa +ded file my $size = $q->upload_info($filename,'size'); # size of uploaded f +ile # short and sweet upload my $ok = $q->upload( $q->param('upload_file'),"$upload_dir/$filename") +; print "Uploaded ".$q->param('upload_file')." and wrote it OK!\n" if $o +k; print "total files = $files<br> filenames = @files<br> filename = $filename<br> mimetype= $mime<br> size=$size<br>";

      The limit is not working because (just like with CGI.pm) you have to set the upload size and disable upload stuff before you get to the use. This is becasue when you call use these modules do the actual parsing of the data stream. The correct order (CGI.pm or CGI::Simple.pm) is:

      #!/usr/bin/perl use warnings; use strict; # set the stuff $CGI::Simple::POST_MAX = 1024; # max upload via post default 100 +kB $CGI::Simple::DISABLE_UPLOADS = 0; # now when we can load the module and parse the data # it will notice the stuff we set above use CGI::Simple; # if we set POST_MAX or DISABLE_UPLOADS here it is too late....

      cheers

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

        Tachyon I notice that the CGI:Simple docs contradict this (very old) node, which is currently correct?

        (From POD)
        use CGI::Simple; $CGI::Simple::DISABLE_UPLOADS = 0; # enable uploads $CGI::Simple::POST_MAX = 1_048_576; # allow 1MB uploads $q = new CGI::Simple;
Re: Re: Re: Re: Yet another CGI Upload problem - not like the others!
by stuayres (Novice) on Dec 11, 2002 at 15:27 UTC
    Hello Tachyon, Firstly, thanks for the help. I'm constantly amazed at how helpful people are & how good the quality of the advice is here. Secondly, I wanted to tell you how far I've got with it! I've tried the first approach & it's given me some feedback, the meaning of which is not clear to me. The file does not appear to be making it to the server, the blessed? reference appeared as follows:
    '.tmpfiles' => { '3' => { 'info' => { 'Content-Disposition' => 'form-data; name="file +"; filename="C:\\stu\\putty\\stu.txt"', 'Content-Type' => 'text/plain' }, 'name' => bless( do{\(my $o = '/usr/tmp/CGItemp10026') +}, 'TempFile' ) } },
    And:
    'file' => [ bless( \*{'Fh::fh00001C%3A\\stu\\putty\\s +tu.txt'}, 'Fh' ) ],
    The error log now records the reason for death as:
    [Wed Dec 11 15:54:14 2002] new_upload.cgi: Use of uninitialized value +in die at <<path to script>>/new_upload.cgi line 70. [Wed Dec 11 15:54:14 2002] new_upload.cgi: Died at <<path to script>>/ +new_upload.cgi line 70. :$
    and line 70 is:
    while($bytes=(read($fh, $buffer, 1024)or die $q->cgi_error())) { $bytes_read += $bytes; print OUTFILE $buffer; }
    I'm stumped. Does this make the reason for the failure clearer to you? Thanks again for your help.