• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

CGI-Perl file operator if file completely uploaded

On a website that i have built I use the code below to display the time of file.zip so that people can tell if it is a new file to be downloaded.  

$time = (stat("downloads/file.zip"))[9];
   $time_str = localtime($time);
print $time_str;

the problem that i encounter is that the file name is a link to download the file but the time stamp of the file changes as soon as the file begins to be uploaded to the server.  i do not have any control over the file being uploaded to the server.  what i would like to do is something where the file is not downlaodable until it is completly uploaded.  I will do something with an if loop and if the file is completely uploaded then I will make it a link.  
What I need help with is that I need to know how to check in an if loop if the file is completely uploaded.  anyone who could fill me in on the information for this I would greatly appreciate.  
thanks.
0
spoma
Asked:
spoma
  • 8
  • 4
  • 2
  • +2
1 Solution
 
spomaAuthor Commented:
since no one has responded maybe thisis more dificult than i thought originally.  i guess what i am really looking for is how to tell if a file is fully loaded and available to be downloaded.  i know i can use -e to see if it exists but this will not check if the file is available to be downloaded.
0
 
TintinCommented:
I'd do something along the lines of:

1.  Record number of bytes of zipped file to be uploaded.
2.  Upload the zip file with a temporary name.
3.  Check the number of bytes uploaded is equal to size of zip file and rename to actual zip file name.
0
 
spomaAuthor Commented:
i agree Tintin and i would have done something like this but as i stated i don't have any control over the file being uploaded.  theya re actually uploaded by someone else.
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
spomaAuthor Commented:
also just so everyone knows the size of the file varies every time.
0
 
spomaAuthor Commented:
i want to do something like this except the -e does not work.

$filename="downloads/file.zip";
if (-w $filename) {
    print "<a href='link'>";
    $time = (stat("downloads/file.zip"))[9];
      $time_str = localtime($time);
    print $time_str;
    print "</a>";
}
else {
    print "file not ready for download";
}

the only other file operators that i can find are:
-d   file is a directory
-e   file exists
-f   file is ordinary
-o   file is ordinary
-r   file is readable by you
-w  file is writable by you
-x   file is executable by you
-z   file is empty

but none of these seem to meet the criteria of what i am trying to do.  are ther any others that might meet my criteria?

0
 
robysathCommented:
Dirty trick:

Check file size... wait 3 seconds... check file size again.

If they do not match then the file is in the process of being uploaded

Exception is ofcourse when someone uploads the file but for some reason the connection hangs for longer than 3 seconds during upload (I don't think there is a good chance of having this problem eh?).
0
 
spomaAuthor Commented:
robysath that is the same approach that i attempted to take.  it does not seem to be working correctly.  while the file is uploading it is still saying that the file is there.  can anyone see a loophole in my code.  



$filename="downloads/file.zip";
if (-e $filename) {
      $currentsize = -s $filename;
      for ($x=0; $x<100; $x++){
      $pastsize = $currentsize;
      }
      $currentsize = -s $filename;
      if ($currentsize == $pastsize) {
          print "<a href='link'>";
          $time = (stat("downloads/file.zip"))[9];
            $time_str = localtime($time);
          print $time_str;
          print "</a>";
      }
      else {
          print "file not ready for download";
      }
}
else {
    print "file not ready for download";
}
0
 
robysathCommented:
100 is far too short, perl will likely run through that much faster than it takes to upload the next set of data for the file.

Try to actually wait for a certain amount of time, ie:

$temp = `sleep 2`;
0
 
spomaAuthor Commented:
so i take it that $temp = `sleep 2`; will actually sleep for two seconds.  i will give this a try and see if it works.  thanks.
0
 
spomaAuthor Commented:
that method did work.  it was kind of a hack and not really the way i wanted to do it but i guess there isn't a cleaner way to do this.  is there anyone out there that knows of a cleaner way to do this?
0
 
TintinCommented:
Don't use

$temp = `sleep 2`;

No need to fork an external process and attempt to capture non-existant output into a variable.

Simply do

sleep 2;
0
 
spomaAuthor Commented:
ya i had done some research on the internet and used sleep (2); and it seems to be working fine.  i just hate having to put a 2 second delay in the time it takes for the webpage to load.
0
 
robysathCommented:
Oh cool, didn't know that existed.... the wonders of perl :)

Thanks Tintin.
0
 
_nn_Commented:
Just an idea... Do you have the option to move the file to another folder ? Usually, a file opened and being written to cannot be moved until it's closed. You could maybe use that fact to "detect" the end of the upload operation.
0
 
ahoffmannCommented:
if your web server does not take care of the upload, either in using exclusive locks, or tmp files, there is no reliable way of identifying when the upload finished.
0
 
robysathCommented:
You could also consider this:

Get the date/time on the file, get the current date/time

Then make sure the file is at least some seconds/minutes/hours/days old before someone can download it from the web.
0
 
ahoffmannCommented:
disagreed.
the only reliable solution are filelocks of the filesystem (and don't forget to beat the NFS-dragons)-: or hand-made locks somehow ... sleep is unreliable.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now