file upload - get the file size problem

Z

zhilianghu

I Google'd it but seems this problem was not previously seen:

I use CGI.pm to upload files through a perl-CGI program:

use CGI;
..... ...
while ($bytesread=read($filehand,$buffer,2048)) {
binmode SAVE;
print SAVE "$buffer";
}

The file upload worked fine, but the "$bytesread" is zero. What could
be the problem? Any hint to diagnose? -- Thanks in advance!

Joe
 
X

xhoster

I Google'd it but seems this problem was not previously seen:

I use CGI.pm to upload files through a perl-CGI program:

use CGI;
.... ...
while ($bytesread=read($filehand,$buffer,2048)) {
binmode SAVE;
print SAVE "$buffer";
}

The file upload worked fine,

Even though you never opened SAVE or $filehand?
but the "$bytesread" is zero. What could
be the problem?

At what point is $bytesread zero? Why do you think that this is a problem?
If $bytesread were not zero (or empty string or undefined) at some point,
how would you ever get out of the while loop?

And why do you binmode SAVE inside the loop?

Xho
 
P

Paul Lalli

I Google'd it but seems this problem was not previously seen:

I'm extremely curious what you "Google'd" that could lead you to the
conclusion that you've found a completely new problem.
I use CGI.pm to upload files through a perl-CGI program:

use CGI;
.... ...
while ($bytesread=read($filehand,$buffer,2048)) {
binmode SAVE;
print SAVE "$buffer";

perldoc -q quoting
}

The file upload worked fine, but the "$bytesread" is zero. What could
be the problem?

Of course it's zero *after* the upload has completed. That's how the
while() loop works. Each time through, it reads up to 2048 bytes from
$filehand, and sets $bytesread to however many bytes it read *that
time*. When there's nothing left to read, read() returns 0,a nd sets
$bytesread to 0, so the while loop ends.
Any hint to diagnose?

Diagnose *what*? Everything is operating exactly as it's supposed to.
If you want to get the total size of all the bytes read, add $bytesread
to a running total each time through.

Paul Lalli
 
J

J. Gleixner

I Google'd it but seems this problem was not previously seen:

That's because it's not a problem.
I use CGI.pm to upload files through a perl-CGI program:

use CGI;
.... ...
while ($bytesread=read($filehand,$buffer,2048)) {
binmode SAVE;
print SAVE "$buffer";
}

The file upload worked fine, but the "$bytesread" is zero. What could
be the problem? Any hint to diagnose? -- Thanks in advance!

How do you know that? The above code doesn't show when you're looking
at the value of $bytesread.

Just a guess, but if you're looking for it to hold the total number of
bytes, it won't, it simply holds the number of bytes returned from the
read(), when it doesn't read anything, the while loop is exited. Similar to:

my $loop = 2;
while( $loop ) { print "In while() loop=$loop\n"; $loop--; }
print "After while() loop = $loop\n";


If you want it to contain the total number of bytes, then you'll need
another variable and you'll have to add $bytesread to it within the while.
 
Z

zhilianghu

Many thanks to each and everyone of you who took time to reply - I
revised my script and it's working in the way I wanted. I learnt more
than expected :)

I didn't realize the "$bytesread" in the construct is "consumed" by the
"while" evaluation loop; and had a hard time to find the resource for
this, and for things like how the syntexis is defined in
"read($filehand,$buffer,2048)", from my 7 good perl books from
O'Reilly. Online materials/tutorials are great and learning by
examples is most efficient for me, but sometimes I fell into
embarrassment before experts ;-)

Usenet group is great. All your input are most appreciated.

Joe
 
P

Paul Lalli

Many thanks to each and everyone of you who took time to reply - I
revised my script and it's working in the way I wanted. I learnt more
than expected :)

I didn't realize the "$bytesread" in the construct is "consumed" by the
"while" evaluation loop;

You're still not getting it. $bytesread is not "consumed" and its
value is not in any way set by the while loop.

The condition of the while loop is the assignment
$bytesread = read($filehand, $buffer, 2048);
The assignment "returns" whatever value is assigned to $bytesread.
Each time the condition is evaluated, read() is called. read() returns
the number of bytes read from $filehand, which will be between 0 and
2048. If read() read any bytes at all, $bytesread will get a non-zero
value, and therefore be true. In that case, the the while loop
executes, because it's condition is true. When read() finally returns
0, $bytesread is 0, which is a false value, and so the while condition
is false and the loop ends.
and had a hard time to find the resource for
this, and for things like how the syntexis is defined in
"read($filehand,$buffer,2048)",

Did you try:
perldoc -f read
?
from my 7 good perl books from O'Reilly.

Did you try the chapter in the Camel (which I have to assume you count
among those seven) which documents each and every Perl built in
function, including read()?
Online materials/tutorials are great and learning by
examples is most efficient for me, but sometimes I fell into
embarrassment before experts ;-)

Forgive me if this is rude, but it does not take an "expert" to read
the documentation for the function you're using.

Paul Lalli
 
T

Tad McClellan

Paul Lalli said:
$bytesread = read($filehand, $buffer, 2048);
The assignment "returns" whatever value is assigned to $bytesread.


s/"returns"/evaluates to/; # now no need for the quotes :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top