Re: Ascertaing uploaded file size

Discussion in 'Perl Misc' started by Justin C, Apr 24, 2013.

  1. Justin C

    Justin C Guest

    On 2013-04-24, Joey@still_Learning.invalid <Joey@still_Learning.invalid> wrote:
    > Thanks for the reply. I'm sorry I presumed that various strings and
    > variables origins weren't identified. I would think that in looking at the
    > basic methodology, one would not have to detail everything.
    >
    > At any rate, I decided it was safest to test files after all files were
    > uploaded.


    I'm not certain that helps you. Doesn't the reported size of the file
    depend on such things as the file system on the remote machine, and
    the block-size used?

    I would have thought the best way to test files are the same (and you
    need them to be the same don't you, not just the same size?) would be
    to use md5sum - this may be overkill, others may suggest a better
    way.

    I think, what I would do (with my limited perl skillset) is to upload
    all the files and have a program on the remote machine calculate the
    md5sum of the files and store that in a file. I DL that file and
    compare the md5sums with ones calculated locally.


    Justin.

    --
    Justin C, by the sea.
    Justin C, Apr 24, 2013
    #1
    1. Advertising

  2. Justin C

    Justin C Guest

    On 2013-04-24, Ben Morrow <> wrote:
    >
    > Quoth Justin C <>:
    >> On 2013-04-24, Joey@still_Learning.invalid <Joey@still_Learning.invalid> wrote:
    >> > Thanks for the reply. I'm sorry I presumed that various strings and
    >> > variables origins weren't identified. I would think that in looking at the
    >> > basic methodology, one would not have to detail everything.
    >> >
    >> > At any rate, I decided it was safest to test files after all files were
    >> > uploaded.

    >>
    >> I'm not certain that helps you. Doesn't the reported size of the file
    >> depend on such things as the file system on the remote machine, and
    >> the block-size used?

    >
    > No. The amount of disk space used does, but (under normal OSen) files
    > have a size which depends only on how much data has been written to
    > them.


    Oh, OK. Thank you for the clarification.


    >> I think, what I would do (with my limited perl skillset) is to upload
    >> all the files and have a program on the remote machine calculate the
    >> md5sum of the files and store that in a file. I DL that file and
    >> compare the md5sums with ones calculated locally.

    >
    > Well, in that situation I'd just use rsync...


    Of course, you *could* just do that ;-)


    Justin.

    --
    Justin C, by the sea.
    Justin C, Apr 24, 2013
    #2
    1. Advertising

  3. "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:

    [...]


    > for (my $j = 0; $j < $imgCnt; $j++) {
    > $filename = $uploadFiles[$j];
    > $filesize = -s $filename
    >
    > opendir DH, $filePath or die "Can't opendir $filePath $!";
    > while (my $serverFileName = readdir DH) {
    > $serverFileSize = -s $serverFileName;
    > if (($serverFileName eq $filename) && ($filesize == $serverFileSize)) {
    > $numFilesReceived++;
    > }}}
    > if ($numFilesReceived != $imgCnt) {$uploadErrorFlag = 1;}
    >
    > BTW, I have a specific purpose in using the loop syntax shown [instead of
    > 'for my $filename (@uploadFiles)']


    Even if this is so, you could use

    for my $j (0 .. $imgCnt - 1) {
    ..
    ..
    ..
    ..
    }

    instead. IMO, you should also consider to make that

    for (0 .. $imgCnt - 1) {
    }

    and use $_ in the loop instead of $j. Another idea would to read the
    directory once into an hash:

    my ($dh, %files);

    opendir($dh, $filePath);
    %files = map { $_, 1 } <$dh>

    and then change the loop to

    for (0 .. $imgCnt - 1) {
    $filename = $uploadFiles[$j];
    ++numFilesReceived if $files{$filename};
    }
    Rainer Weikusat, Apr 25, 2013
    #3
  4. Justin C

    C.DeRykus Guest

    On Thursday, April 25, 2013 7:19:47 AM UTC-7, Jo...@still_Learning.invalid wrote:
    > Rainer Weikusat wrote:
    >
    >
    >
    > >"Joey@still_Learning.invalid" <Joey@still_Learning.invalid> writes:

    >
    > >

    >
    > >[...]

    >
    > >

    >
    > >

    >
    > >> for (my $j = 0; $j < $imgCnt; $j++) {

    >
    > ...<snip>
    >
    > >>

    >
    > >> BTW, I have a specific purpose in using the loop syntax shown [instead of

    >
    > >> 'for my $filename (@uploadFiles)']

    >
    > >

    >
    > >Even if this is so, you could use

    >
    > >

    >
    > >for my $j (0 .. $imgCnt - 1) {

    >
    > >.

    >
    > >instead. IMO, you should also consider to make that

    >
    > >

    >
    > >for (0 .. $imgCnt - 1) {

    >
    > >}

    >
    > >

    >
    > >and use $_ in the loop instead of $j. Another idea would to read the

    >
    > >directory once into an hash:

    >
    > >

    >
    > >my ($dh, %files);

    >
    > >

    >
    > >opendir($dh, $filePath);

    >
    > >%files = map { $_, 1 } <$dh>

    >
    > >

    >
    > >and then change the loop to

    >
    > >

    >
    > >for (0 .. $imgCnt - 1) {

    >
    > > $filename = $uploadFiles[$j];

    >
    > > ++numFilesReceived if $files{$filename};

    >
    > >}

    >
    > Thank you for the suggestions. More for me to ponder...
    >


    Here's how a more idiomatic, slimmed-down, calorie-counting
    version of the code might look:

    ....
    for (0 .. $imgCnt-1) {

    open my $upload_fh, '>', "$filePath/$uploadFiles[$_]"
    or die...
    binmode $upload_fh;
    print $upload_fh while <$upload_filehandle>;
    close $upload_fh or die ...;
    }

    opendir my $dir_fh, $filePath or die ...;

    for (0 .. $imgCnt-1) {

    while (my $serverFileName = readdir $dir_fh) {
    $numImagesReceived++ if $serverFileName eq uploadFiles[$_]
    and -s $serverFileName == -s $uploadFiles[$_];
    }
    seekdir( $dir_fh, 0 ) or die "seek: $uploadFiles[$_];
    }
    if ($numImagesReceived != $imgCnt) {$uploadErrorFlag = 1;}
    ....


    --
    Charles DeRykus
    C.DeRykus, Apr 25, 2013
    #4
  5. On 4/25/2013 1:04 PM, C.DeRykus wrote:
    > On Thursday, April 25, 2013 7:19:47 AM UTC-7, Jo...@still_Learning.invalid wrote:
    >> Rainer Weikusat wrote:
    >>
    >>
    >>
    >>> "Joey@still_Learning.invalid" <Joey@still_Learning.invalid

    ....

    > Here's how a more idiomatic, slimmed-down, calorie-counting
    > version of the code might look:
    >
    > ...


    I need to slim down as well... mea culpa for the double spacing on the
    earlier post.

    --
    Charles DeRykus
    Charles DeRykus, Apr 25, 2013
    #5
  6. "Joey@still_Learning.invalid" <Joey@still_Learning.invalid> wrote:
    >My customers must provide a person's name, which has spaces between first
    >and last.


    Or it has a comma between last name and given name ...

    >The filename info is sent as space-separated values, so when I
    >go to output the name... I had to comma-separate to preserve the integrity
    >of the person's name.


    .... in which case your hack does not work any longer.

    Lesson: never assume anything about user-supplied data.

    jue
    Jürgen Exner, Apr 27, 2013
    #6
    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. Matt G
    Replies:
    1
    Views:
    1,143
    Deepak Kumar Vasudevan
    Aug 22, 2003
  2. Johnson

    Limiting Uploaded File Size

    Johnson, Feb 18, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    503
    Lau Lei Cheong
    Feb 19, 2005
  3. RobM
    Replies:
    0
    Views:
    621
  4. h3m4n
    Replies:
    2
    Views:
    576
    h3m4n
    Jul 8, 2006
  5. Peter J. Holzer

    Re: Ascertaing uploaded file size

    Peter J. Holzer, Apr 25, 2013, in forum: Perl Misc
    Replies:
    9
    Views:
    267
    Dave Saville
    Apr 27, 2013
Loading...

Share This Page