Solved

CGI-Perl file operator if file completely uploaded

Posted on 2003-10-30
20
493 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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
 

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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

713 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