Solved

CGI-Perl file operator if file completely uploaded

Posted on 2003-10-30
20
490 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
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 look for a specific file type in a local or remote server directory using PHP.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

743 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

15 Experts available now in Live!

Get 1:1 Help Now