| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
+----------------------------------------------------------------------+
|
|
|
4 |
| Uploadprogress extension |
|
|
|
5 |
+----------------------------------------------------------------------+
|
|
|
6 |
| Copyright (c) 2006-2008 The PHP Group |
|
|
|
7 |
+----------------------------------------------------------------------+
|
|
|
8 |
| This source file is subject to version 3.01 of the PHP license, |
|
|
|
9 |
| that is bundled with this package in the file LICENSE, and is |
|
|
|
10 |
| available through the world-wide-web at the following url: |
|
|
|
11 |
| http://www.php.net/license/3_01.txt. |
|
|
|
12 |
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
13 |
| obtain it through the world-wide-web, please send a note to |
|
|
|
14 |
| license@php.net so we can mail you a copy immediately. |
|
|
|
15 |
+----------------------------------------------------------------------+
|
|
|
16 |
| Author: Christian Stocker (chregu@php.net) |
|
|
|
17 |
+----------------------------------------------------------------------+
|
|
|
18 |
*/
|
|
|
19 |
|
|
|
20 |
$id = md5(microtime() . rand());
|
|
|
21 |
|
|
|
22 |
function return_bytes($val) {
|
|
|
23 |
$val = trim($val);
|
|
|
24 |
$last = strtolower($val[strlen($val)-1]);
|
|
|
25 |
switch($last) {
|
|
|
26 |
// The 'G' modifier is available since PHP 5.1.0
|
|
|
27 |
case 'g':
|
|
|
28 |
$val *= 1024;
|
|
|
29 |
case 'm':
|
|
|
30 |
$val *= 1024;
|
|
|
31 |
case 'k':
|
|
|
32 |
$val *= 1024;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
return $val;
|
|
|
36 |
}
|
|
|
37 |
?>
|
|
|
38 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
|
39 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
|
40 |
|
|
|
41 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
|
42 |
<head>
|
|
|
43 |
<meta name="generator" content="HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org" />
|
|
|
44 |
<script type="text/javascript">
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
var UP = function() {
|
|
|
48 |
|
|
|
49 |
/* private variables */
|
|
|
50 |
|
|
|
51 |
var ifr = null;
|
|
|
52 |
|
|
|
53 |
var startTime = null;
|
|
|
54 |
var upload_max_filesize = <?php echo return_bytes(ini_get('upload_max_filesize'));?>;
|
|
|
55 |
|
|
|
56 |
var infoUpdated = 0;
|
|
|
57 |
|
|
|
58 |
var writeStatus = function(text,color) {
|
|
|
59 |
var statDiv = document.getElementById("status");
|
|
|
60 |
if (color == 1 ) {
|
|
|
61 |
statDiv.style.backgroundColor = "green";
|
|
|
62 |
} else if (color == 2 ) {
|
|
|
63 |
statDiv.style.backgroundColor = "orange";
|
|
|
64 |
} else if (color == 3 ) {
|
|
|
65 |
statDiv.style.backgroundColor = "red";
|
|
|
66 |
} else {
|
|
|
67 |
statDiv.style.backgroundColor = "white";
|
|
|
68 |
}
|
|
|
69 |
statDiv.innerHTML = text;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
|
|
|
73 |
return {
|
|
|
74 |
start: function() {
|
|
|
75 |
ifr = document.getElementById("ifr");
|
|
|
76 |
startTime = new Date();
|
|
|
77 |
infoUpdated = 0;
|
|
|
78 |
this.requestInfo();
|
|
|
79 |
},
|
|
|
80 |
stop: function(files) {
|
|
|
81 |
if (typeof files == 'undefined' || files) {
|
|
|
82 |
var secs = (new Date() - startTime)/1000;
|
|
|
83 |
var statusText = "Upload succeeded, it took " + secs + " seconds. <br/> ";
|
|
|
84 |
if (infoUpdated > 0) {
|
|
|
85 |
writeStatus(statusText + "You had " + infoUpdated + " updates from the progress meter, looks like it's working fine",1);
|
|
|
86 |
} else {
|
|
|
87 |
statusText += "BUT there were no progress meter updates<br/> ";
|
|
|
88 |
if (secs < 3) {
|
|
|
89 |
writeStatus(statusText + "Your upload was maybe too short, try with a bigger file or a slower connection",2);
|
|
|
90 |
} else {
|
|
|
91 |
writeStatus(statusText + "Your upload should have taken long enough to have an progress update. Maybe it really does not work...",3);
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
|
|
|
95 |
|
|
|
96 |
}
|
|
|
97 |
} else {
|
|
|
98 |
writeStatus('PHP did not report any uploaded file, maybe it was too large, try a smaller one (post_max_size: <?php echo ini_get('post_max_size');?>)',3);
|
|
|
99 |
}
|
|
|
100 |
startTime = null;
|
|
|
101 |
},
|
|
|
102 |
requestInfo: function() {
|
|
|
103 |
ifr.src="info.php?ID=<?php echo $id;?>&"+new Date();
|
|
|
104 |
},
|
|
|
105 |
|
|
|
106 |
updateInfo: function(uploaded, total, estimatedSeconds) {
|
|
|
107 |
if (startTime) {
|
|
|
108 |
if (uploaded) {
|
|
|
109 |
infoUpdated++;
|
|
|
110 |
if (total > upload_max_filesize) {
|
|
|
111 |
writeStatus("The file is too large and won't be available for PHP after the upload<br/> Your file size is " + total + " bytes. Allowed is " + upload_max_filesize + " bytes. That's " + Math.round (total / upload_max_filesize * 100) + "% too large<br/> Download started since " + (new Date() - startTime)/1000 + " seconds. " + Math.floor(uploaded / total * 100) + "% done, " + estimatedSeconds + " seconds to go",2);
|
|
|
112 |
} else {
|
|
|
113 |
writeStatus("Download started since " + (new Date() - startTime)/1000 + " seconds. " + Math.floor(uploaded / total * 100) + "% done, " + estimatedSeconds + " seconds to go");
|
|
|
114 |
}
|
|
|
115 |
} else {
|
|
|
116 |
writeStatus("Download started since " + (new Date() - startTime)/1000 + " seconds. No progress info yet");
|
|
|
117 |
}
|
|
|
118 |
window.setTimeout("UP.requestInfo()",1000);
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
}()
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
</script>
|
|
|
129 |
<title> php5.2 uploadprogress Meter - Simple Demo</title>
|
|
|
130 |
|
|
|
131 |
</head>
|
|
|
132 |
|
|
|
133 |
<body>
|
|
|
134 |
<form onsubmit="UP.start()" target="ifr2" action="server.php" enctype="multipart/form-data" method="post">
|
|
|
135 |
<input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
|
|
|
136 |
<label>Select File:</label>
|
|
|
137 |
<input type="file" name="file" />
|
|
|
138 |
<br/>
|
|
|
139 |
<label>Select File:</label>
|
|
|
140 |
<input type="file" name="file2" />
|
|
|
141 |
|
|
|
142 |
<br/>
|
|
|
143 |
|
|
|
144 |
<label>Upload File:</label>
|
|
|
145 |
<input type="submit" value="Upload File" />
|
|
|
146 |
<br/>
|
|
|
147 |
('upload_max_filesize' is <?php echo ini_get('upload_max_filesize');?> per file)<br/>
|
|
|
148 |
|
|
|
149 |
('post_max_size' is <?php echo ini_get('post_max_size');?> per submit)<br/>
|
|
|
150 |
|
|
|
151 |
<?php
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
$templateini = ini_get("uploadprogress.file.filename_template");
|
|
|
155 |
$testid = "thisisjustatest";
|
|
|
156 |
$template = sprintf($templateini,$testid);
|
|
|
157 |
$templateerror = false;
|
|
|
158 |
if ($template && $template != $templateini && @touch ($template) && file_exists($template)) {
|
|
|
159 |
// print '('.$templateini.' is writable. The realpath is ' . str_replace($testid,"%s",realpath($template)) .')';
|
|
|
160 |
unlink($template);
|
|
|
161 |
} else {
|
|
|
162 |
$templateerror = true;
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
?>
|
|
|
166 |
</form>
|
|
|
167 |
<div id="status" style="border: 1px black solid;<?php
|
|
|
168 |
if (function_exists("uploadprogress_get_info")) {
|
|
|
169 |
if ($templateerror) {
|
|
|
170 |
print 'background-color: red;"';
|
|
|
171 |
print ">Problem. ";
|
|
|
172 |
if ($template == $templateini) {
|
|
|
173 |
print "uploadprogress.file.filename_template ($templateini) doesn't have an %s in it for making unique temporary files. Please adjust.<br/>";
|
|
|
174 |
} else {
|
|
|
175 |
print "$templateini is NOT writable. <br/>Please make sure the directory exists and is writable for the webserver. <br/>
|
|
|
176 |
Or adjust the ini setting 'uploadprogress.file.filename_template' to a correct path.";
|
|
|
177 |
}
|
|
|
178 |
} else {
|
|
|
179 |
print 'background-color: green;">The uploadprogress extension is installed and initial checks show everything is good';
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
|
|
|
183 |
} else { ?>
|
|
|
184 |
|
|
|
185 |
background-color: red;">The uploadprogress extension is not installed.
|
|
|
186 |
|
|
|
187 |
|
|
|
188 |
<?php } ?>
|
|
|
189 |
|
|
|
190 |
</div>
|
|
|
191 |
|
|
|
192 |
|
|
|
193 |
|
|
|
194 |
<div>The info during the upload will be displayed here:</div>
|
|
|
195 |
<iframe id="ifr" src="info.php?ID=<?php echo $id;?>" width="500px" height="350px" name="ifr"></iframe>
|
|
|
196 |
|
|
|
197 |
<div>
|
|
|
198 |
|
|
|
199 |
The actual file upload happens here (and displays info, when it's finished):
|
|
|
200 |
</div>
|
|
|
201 |
<iframe name="ifr2" width="500px" height="300px" id="ifr2"></iframe>
|
|
|
202 |
</body>
|
|
|
203 |
|
|
|
204 |
</html>
|