Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > file upload - get the file size problem

Reply
Thread Tools

file upload - get the file size problem

 
 
zhilianghu@gmail.com
Guest
Posts: n/a
 
      05-19-2006
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,204) {
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-19-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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,204) {
> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-19-2006
(E-Mail Removed) wrote:
> 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,204) {
> 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

 
Reply With Quote
 
J. Gleixner
Guest
Posts: n/a
 
      05-19-2006
(E-Mail Removed) wrote:
> 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,204) {
> 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.
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-19-2006
(E-Mail Removed) wrote:
> I use CGI.pm to upload files through a perl-CGI program:


<snip>

> The file upload worked fine, but the "$bytesread" is zero. What could
> be the problem? Any hint to diagnose?


To identify/prevent common mistakes in connection with file uploads, use
the CPAN module CGI::UploadEasy.

http://search.cpan.org/perldoc?CGI%3A%3AUploadEasy

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
zhilianghu@gmail.com
Guest
Posts: n/a
 
      05-19-2006
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,204", 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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-19-2006

(E-Mail Removed) wrote:
> 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, 204;
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,204",


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

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      05-21-2006
Paul Lalli <(E-Mail Removed)> wrote:

> $bytesread = read($filehand, $buffer, 204;
> The assignment "returns" whatever value is assigned to $bytesread.



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


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
File upload from client application (non-form based upload) stuart@microsoft.com Python 1 11-25-2006 12:14 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
How to detect sizeof upload in order to show status of browser file upload? Heather Fraser Java 0 07-05-2004 12:35 AM



Advertisments