file upload - get the file size problem

Discussion in 'Perl Misc' started by zhilianghu@gmail.com, May 19, 2006.

  1. Guest

    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
     
    , May 19, 2006
    #1
    1. Advertising

  2. Guest

    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,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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , May 19, 2006
    #2
    1. Advertising

  3. Paul Lalli Guest

    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,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
     
    Paul Lalli, May 19, 2006
    #3
  4. J. Gleixner Guest

    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,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.
     
    J. Gleixner, May 19, 2006
    #4
  5. Gunnar Hjalmarsson, May 19, 2006
    #5
  6. Guest

    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
     
    , May 19, 2006
    #6
  7. Paul Lalli Guest

    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, 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
     
    Paul Lalli, May 19, 2006
    #7
  8. Paul Lalli <> wrote:

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



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 21, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2FyYXY=?=

    Upload a file without file Upload control - ASP.Net

    =?Utf-8?B?U2FyYXY=?=, Aug 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    2,472
    Bruce Barker
    Aug 3, 2005
  2. Heather Fraser
    Replies:
    0
    Views:
    643
    Heather Fraser
    Jul 5, 2004
  3. Replies:
    1
    Views:
    948
    Gabriel Genellina
    Nov 25, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,607
    Michael Jung
    May 25, 2008
  5. Prakash
    Replies:
    3
    Views:
    468
    Ray at
    Nov 12, 2003
Loading...

Share This Page