-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlib_upload.php
142 lines (132 loc) · 3.8 KB
/
lib_upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?
/* $uploadURL
* IE:
* The $uploadURL page must output a visible, normal page.
* On success, it must refresh the opener and close itself.
* It must show an error message otherwise.
* non IE:
* The $uploadURL page must output plain text.
* Must return "OK" on success.
* Or an error text otherwise.
*
* ----- The rest of parameters are ignored in IE -----
*
* $aMIME is an array of MIME types -- example: array("image/gif","image/jpeg")
*
* $maxSize is in Kb
*
* $accept is a string of comma separated MIME types -- examples: "image/*" or "image/gif,image/jpeg"
*
* $onUploaded is a string with JS instructions to be executed on suceessful upload.
* examples:
* "window.location.reload();"
* "uploaded();" // function uploaded() must exist in your code
*/
function echoUploadForm($uploadURL,$aMIME,$maxSize,$accept,$onUploaded)
{
$IE = (preg_match("/msie/i",$_SERVER["HTTP_USER_AGENT"]) || preg_match("/internet explorer/i",$_SERVER["HTTP_USER_AGENT"]));
if (!$IE)
{
$jsaMIME = "";
foreach($aMIME as $m)
{
if ($jsaMIME) $jsaMIME .= "','";
$jsaMIME .= $m;
}
$jsaMIME = "['$jsaMIME']";
echo "<script>
var xhr;
function uploadFile()
{
var MIMEtypes = $jsaMIME;
var fileInput = document.getElementById('upldfile');
if (fileInput.files.length == 0)
alert('No file selected');
else if (fileInput.files.length > 1)
alert('Uploading more than one file is not permitted');
else
{
var file = fileInput.files[0];
for (var x=0;x<MIMEtypes.length;x++)
if (file.type == MIMEtypes[x])
break;
if (x == MIMEtypes.length)
{
var msg = 'Type of file not supported';
if (file.type)
msg += ' ('+file.type+')';
msg += '.';
alert(msg);
}
else if (file.size > $maxSize*1024)
alert('Size of file exceeds maximum allowed ($maxSize Kb).');
else
{
progressbarReset();
xhr = new XMLHttpRequest();
xhr.onreadystatechange = progressbarReadyStateChange;
xhr.upload.addEventListener('progress', onprogressHandler, false);
xhr.open('POST', '$uploadURL', true);
var formData = new FormData();
formData.append('file', file);
xhr.send(formData);
}
}
}
function progressbarPosition(pct)
{
document.getElementById('progressbarIn').style.marginLeft = (pct*2)+'px';
}
function progressbarReset()
{
progressbarPosition(0);
document.getElementById('submit').disabled = true;
document.getElementById('upldfile').disabled = true;
document.getElementById('progressbarOut').style.display = 'block';
}
function progressbarHide()
{
document.getElementById('progressbarOut').style.display = 'none';
document.getElementById('submit').disabled = false;
document.getElementById('upldfile').disabled = false;
}
function onprogressHandler(evt)
{
progressbarPosition (Math.round(evt.loaded/evt.total*100));
}
function progressbarReadyStateChange()
{
if (xhr.readyState == 4)
{
var response = xhr.responseText.replace(/[\\s\\r\\n]+$/,'');
if (response == 'OK')
{
$onUploaded
}
else
{
progressbarHide();
alert(response);
}
}
}
</script>
<input type=file id=upldfile name=file accept='$accept'/>
<input type=button id=submit onclick='uploadFile();' value=submit />
<p>
<span id=progressbarOut style='display:none;width:200px;height:5px;background-color:red;overflow-x:hidden;'>
<span id=progressbarIn style='width:200px;height:5px;display:block;background-color:#eee;'></span>
</span>";
}
else
{
echo "<script>
var wupload = 0;
</script>
<form action='$uploadURL' target='w_upload' onsubmit='if(wupload && !wupload.closed) wupload.close(); wupload=window.open(\"\", \"w_upload\", \"width=200, height=20, location=0, scrollbars=0, resizable=0\"); wupload.moveTo((screen.availWidth-200)/2,(screen.availHeight-20)/2);' method='post' enctype='multipart/form-data'>
<input type='file' name='file' />
<input type='submit' name='submit' value='submit' />
</form>";
}
}
?>