3
# PHP File Uploader with progress bar Version 1.43
4
# Copyright (C) Raditha Dissanyake 2003,2004
5
# http://www.raditha.com
8
# The contents of this file are subject to the Mozilla Public
9
# License Version 1.1 (the "License"); you may not use this file
10
# except in compliance with the License. You may obtain a copy of
11
# the License at http://www.mozilla.org/MPL/
13
# Software distributed under the License is distributed on an "AS
14
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15
# implied. See the License for the specific language governing
16
# rights and limitations under the License.
18
# The Initial Developer of the Original Code is Raditha Dissanayake.
19
# Portions created by Raditha are Copyright (C) 2003,2004
20
# Raditha Dissanayake. All Rights Reserved.
22
# Portions contributed by Orest Kinasevych are Copyright (C)
28
# No longer uses cookies. This has two major benefits; the first
29
# being that minority of users who do not like cookies are not
30
# inconvinienced. Secondly there is one less dependecy and this
31
# would lead to a smoother setup for most people.
32
# (if you want to use cookies look at the contrib folder)
35
# added a cache control header in version 1.02
38
# Added a more detailed progress bar in version
41
# The organization of the temporary files have been improved so to
42
# make is easier to clean up after abandoned uploads. (as suggested
50
use Fcntl qw(:DEFAULT :flock);
53
# Carp is only needed if you want debugging. Uncomment the above line
54
# if you comment out any of the carp statements in the body of the
59
# the most obvious issue that we will face is file locking.
60
# if two threads read and write from the same file at the same
61
# time only one of them will be allowed to finish the operation.
62
# Since we are storing our temporary data in a file we are likely to
63
# run into that exact same problem.
64
# We can't overcome it but we can make sure the progress bar does
65
# not display junk when that happens.
68
# status codes = 0-uploading, 1-started, 2- complete
72
$sessionid = $query->param('sessionid');
73
$sessionid =~ s/[^a-zA-Z0-9]//g; # santized as suggested by Terrence Johnson.
75
$iTotal = $query->param('iTotal');
76
$iRead = $query->param('iRead');
77
$status = $query->param('iStatus');
81
# The code that deals with calculating elapsed time, time remaining
82
# and upload speed were contributed by Orest Kinasevych
86
# Get values assigned for current time and upload start time
88
#$dtnow = $query->param('dtnow'); # assign value for current time
90
$dtstart = $query->param('dtstart'); # assign value for upload start time
94
#carp "$dtnow $dtstart";
96
$thisUrl = $query->url;
100
# Calculate elapsed time and format for display
102
$dtelapsed = $dtnow - $dtstart;
103
$dtelapsed_sec = ($dtelapsed % 60); # gets number of seconds
104
$dtelapsed_min = ((($dtelapsed - $dtelapsed_sec) % 3600) / 60); # gets number of minutes
105
$dtelapsed_hours = (((($dtelapsed - $dtelapsed_sec) - ($dtelapsed_min * 60)) % 86400) / 3600);
106
# gets number of hours; assuming that we won't be going into days!
107
if ($dtelapsed_sec < 10) { $dtelapsed_sec = "0$dtelapsed_sec"; } # append leading zero
108
if ($dtelapsed_min < 10) { $dtelapsed_min = "0$dtelapsed_min"; } # append leading zero
109
if ($dtelapsed_hours < 10) { $dtelapsed_hours = "0$dtelapsed_hours"; } # append leading zero
110
$dtelapsedf = "$dtelapsed_hours:$dtelapsed_min:$dtelapsed_sec"; # display as 00:00:00
116
$bSpeed = 0; # if not yet determined
117
if ($dtelapsed > 0) # avoid divide by zero errors
119
$bSpeed = $iRead / $dtelapsed; # Bytes uploaded / Seconds elapsed = Bytes/Second speed
120
$bitSpeed = $bSpeed * 8; # bps
121
$kbitSpeed = $bitSpeed / 1000; # Kbps
125
$kbitSpeed = $bSpeed; # just pass the zero value
127
$bSpeedf = sprintf("%d",$kbitSpeed); # remove decimals
132
# Calculate remaining time based on upload speed so far
135
$bRemaining = $iTotal - $iRead; # Total size - amount uploaded = amount remaining
138
# Bytes remaining / Bytes/Second = Seconds
139
$dtRemaining = $bRemaining / $bSpeed;
141
$dtRemaining = sprintf("%d",$dtRemaining); # remove decimals
142
$dtRemaining_sec = ($dtRemaining % 60); # gets number of seconds
143
$dtRemaining_min = ((($dtRemaining - $dtRemaining_sec) % 3600) / 60); # gets number of minutes
144
$dtRemaining_hours = (((($dtRemaining - $dtRemaining_sec) - ($dtRemaining_min * 60)) % 86400) / 3600); # gets number of hours; assuming that we won't be going into days!
145
if ($dtRemaining_sec < 10)
147
# append leading zero
148
$dtRemaining_sec = "0$dtRemaining_sec";
150
if ($dtRemaining_min < 10)
152
# append leading zero
153
$dtRemaining_min = "0$dtRemaining_min";
155
if ($dtRemaining_hours < 10)
157
# append leading zero
158
$dtRemaining_hours = "0$dtRemaining_hours";
160
$dtRemainingf = "$dtRemaining_hours:$dtRemaining_min:$dtRemaining_sec"; # display as 00:00:00
163
# The values for iStatus are
169
#carp "iTotal = $iTotal, iRead = $iRead, status = $status, sessionId = $sessionid";
171
require("./header.cgi");
177
if(open (STAT, $monitor_file))
179
sysopen(STAT, $monitor_file, O_RDONLY)
180
or die "can't open numfile: $!";
181
$ofh = select(STAT); $| = 1; select ($ofh);
184
#carp "trying to read the stuff in $iTotal";
185
if(defined($iTotal) && $iTotal ne "")
200
# many thanx to Terrence Johnson who pointed out the fact that i should have added
201
# cache control header.
204
print "Pragma: no-cache\n";
205
print "Content-type: text/xml\n\n ";
216
# we will try to read in the total size of data to be transfered from the
217
# shared file. It will also tell us how much data has been transfered upto
220
$bRead = -s "$post_data_file";
224
# We have been able to read in it from the file.
225
$percent = $bRead * 100 / $iTotal;
236
# division results in truncation errors at times so don't compare percentage
237
# There have been occaisional reports of the progress bar showing 100% but not
238
# disappearing even after file upload has been completed.
240
# Nils Menrad came up with the solution which is to modify the end of upload
243
if((($iTotal == $bRead) && $bRead != 0) || $bRead>$iTotal)
245
if($status == 1 && -e "$signal_file")
254
unlink $monitor_file;
255
unlink $post_data_file;
263
$kachal = "$bRead , $iTotal";
267
&make_progress_bar();
276
# Since the progress bar is in html, so it needs to refresh itself periodicaly to
277
# obtain new values. The refresh url with the query string is generated by this
283
#print "Content-type: text/html\n\n ";
284
#print "hellow $iTotal $iStatus $sessionid $iRead <br>\n" ;
287
$url = "$thisUrl?iTotal=$iTotal&iRead=$iRead&iStatus=$status&sessionid=$sessionid&dtnow=$dtnow&dtstart=$dtstart";
295
sub make_progress_bar
300
<UploadProgress sessionID="$sessionid">
301
<RefreshURL><![CDATA[$url]]></RefreshURL>
302
<TotalBytes>$iTotal</TotalBytes>
303
<ReadBytes>$iRead</ReadBytes>
304
<Status>$status</Status>
305
<Speed>$bSpeedf</Speed>
306
<TimeRemaining>$dtRemainingf</TimeRemaining>
307
<TimeElapsed>$dtelapsedf</TimeElapsed>
321
<UploadProgress sessionID="$sessionid">
322
<RefreshURL><![CDATA[$url]]></RefreshURL>
323
<TotalBytes>$iTotal</TotalBytes>
324
<ReadBytes>$iRead</ReadBytes>
325
<Status>$status</Status>
326
<Speed>$bSpeedf</Speed>
327
<TimeRemaining>0</TimeRemaining>
328
<TimeElapsed>$dtelapsedf</TimeElapsed>
337
if(readFlength() == 1)
344
<UploadProgress sessionID="$sessionid">
345
<RefreshURL><![CDATA[$url]]></RefreshURL>
346
<TotalBytes>$iTotal</TotalBytes>
347
<ReadBytes>$iRead</ReadBytes>
348
<Status>$status</Status>
349
<Speed>$bSpeedf</Speed>
350
<TimeRemaining>$dtRemainingf</TimeRemaining>
351
<TimeElapsed>$dtelapsedf</TimeElapsed>
360
<UploadProgress sessionID="$sessionid">
361
<RefreshURL><![CDATA[$url]]></RefreshURL>
362
<TotalBytes>$iTotal</TotalBytes>
363
<ReadBytes>$iRead</ReadBytes>
365
<Speed>$bSpeedf</Speed>
366
<TimeRemaining>$dtRemainingf</TimeRemaining>
367
<TimeElapsed>$dtelapsedf</TimeElapsed>
373
# this function may not return;
374
sub get_last_values()
395
# we have done everything possible to try to retrieve the data
396
# now try to calculate the percentage once again
401
if(defined($iTotal) && $iTotal != 0)
403
$percent = $bRead * 100 / $iTotal;