Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Short read error?

by JimJx (Beadle)
on Feb 19, 2007 at 19:28 UTC ( [id://600931]=perlquestion: print w/replies, xml ) Need Help??

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

Hi all,

After thinking I had my probs straightened out, I ran into one I can't really seem to find any info on....

When running the script below, I get Short Read: wanted 437, got 0

My brain, which is admittedly a little fried today, guesses that the script is not getting the bytes it expects. Hence the "wanted 438, got 0" part.

The question I have now, is can anyone spot where the problem lies or know what can cause this error?

Thanks!
Jim

#!/usr/local/bin/perl use CGI; $query = new CGI; print $query->header; print $query->start_html(-title=>"UPLOAD PLEASE"); # retrieve the upload file name. unless ($file = $query->param('fileID')) { &showError('No file name specified.'); } @pathName = split(/\\/,$file); $newFile ='/home/users/web/b2578/pow.royridgeway/htdocs/Roy/'; $newFile .= pop(@pathName); open(OPF,">$newFile") || &showError("Failed to open OPF, $!"); print "<P><CENTER>Uploading <BR>$fileID <BR>to<BR>$newFile<BR></CENTER +></P>\n"; while ($bytesread=read($file,$buffer,1024)) { print OPF "$buffer"; } close OPF; print $query->end_html; }

Replies are listed 'Best First'.
Re: Short read error?
by GrandFather (Saint) on Feb 19, 2007 at 19:47 UTC

    Always use strictures: use strict; use warnings;. If you turn on strictures for your code and declare everything where it is initialized, you will find that the print uses the undeclared variable $fileID which may or may bot be your problem, but isn't good in any case.


    DWIM is Perl's answer to Gödel
      I went in and added the use strict and declared everything, same error.

      One interesting point, since I got the exact same error message, I went in and removed a couple vars from the declarations just too see what would happen. It didn't choke on that as I expected it to do so it seems that wherever the problem lies, it isn't even getting that far.....

Re: Short read error?
by Anonymous Monk on Feb 19, 2007 at 23:37 UTC
Re: Short read error?
by cdarke (Prior) on Feb 19, 2007 at 19:51 UTC
    I can't see an open for the input file, but presumably the file descriptor is coming from the query string? read usually returns zero when the file descriptor is at EOF. So either the file is empty, or the script that opened the file read it all, in which case you need to seek $file,0,0.
      OK, I hate to admit this, but you lost me. Where would I need to add the seek at?
        Do the seek before the read
Re: Short read error?
by varian (Chaplain) on Feb 20, 2007 at 08:13 UTC
    Most likely the Short Read error is thrown because you did not read the content data that was uploaded. Subsequently the webserver tries to read a command but gets data from its incoming stream.
    $file = $query->param('fileID')
    This will get you the filename that the HTTP client suggested, not a handle to the input stream. Therefore the "read($file,$buffer,1024)" command fails and not all of the incoming data stream is read.
    my $fh = $query->upload('fileID');
    Gets you a proper input filehandle, then use read($fh,$buffer,1024) as per your code.

    I would recommend to also set binmode($fh) as well as on the output file so that you can safely process non-text files.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://600931]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-25 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found