Solved

CGI-Perl file operator if file completely uploaded

Posted on 2003-10-30
20
491 Views
Last Modified: 2013-12-25
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
Comment
Question by:spoma
  • 8
  • 4
  • 2
  • +2
20 Comments
 

Author Comment

by:spoma
ID: 9653616
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
 
LVL 48

Expert Comment

by:Tintin
ID: 9654160
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
 

Author Comment

by:spoma
ID: 9654254
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
 

Author Comment

by:spoma
ID: 9654290
also just so everyone knows the size of the file varies every time.
0
 

Author Comment

by:spoma
ID: 9654435
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
 
LVL 1

Expert Comment

by:robysath
ID: 9654885
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
 

Author Comment

by:spoma
ID: 9658217
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
 
LVL 1

Expert Comment

by:robysath
ID: 9658716
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:spoma
ID: 9658840
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
 

Author Comment

by:spoma
ID: 9660129
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
 
LVL 48

Expert Comment

by:Tintin
ID: 9660160
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
 

Author Comment

by:spoma
ID: 9660759
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
 
LVL 1

Expert Comment

by:robysath
ID: 9661016
Oh cool, didn't know that existed.... the wonders of perl :)

Thanks Tintin.
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9661115
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9662947
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
 
LVL 1

Expert Comment

by:robysath
ID: 9663285
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 11999881
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading variable length EBCDIC in SAS 9 98
Powershell Find Folders 7 52
powershell script help 5 73
Configure Robocopy to excluding folders 6 46
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now