-
Notifications
You must be signed in to change notification settings - Fork 988
/
uploader.php
330 lines (281 loc) · 13.9 KB
/
uploader.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Advanced File Uploader</title>
<script type="text/javascript" src="scripts/jquery/jquery.js"></script>
<script type="text/javascript" src="scripts/ajaxupload.js"></script>
<link type="text/css" href="scripts/jquery/css/jquery-ui-1.8.1.custom.css" rel="stylesheet" />
<script type="text/javascript" src="scripts/jquery/jquery-ui.js"></script>
<style type="text/css">
body {font-family: verdana, arial, helvetica, sans-serif;font-size: 12px;color: black; direction: ltr;}
h1 {color: #C7D92C; font-size: 18px; font-weight: 400;}
a { color: white;}
a:hover, a.hover {color: #C7D92C;}
ul { list-style: none; }
#dialog-confirm { padding: 0 20px; float: left; width: 750px; }
div.button {
height: 29px;
width: 133px;
background: url(images/button.png) 0 0;
margin: 50px auto;
font-size: 14px; color: #C7D92C; text-align: center; padding-top: 15px;
}
/*
We can't use ":hover" preudo-class because we have
invisible file input above, so we have to simulate
hover effect with JavaScript.
*/
div.button.hover {
background: url(images/button.png) 0 56px;
color: #95A226;
}
.notice {
background: #FFF6BF;
border: 2px solid #FFD324;
text-align: center;
margin: 10px auto;
padding: 5px 20px;
color: red;
}
.previewblock {
background-color: #99CCFF;
margin: 10px auto;
padding: 5px 20px;
}
img {
display: block;
margin: 5px;
border: 0 none;
border-style: solid;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
var ia = $('#ia').val();
$('#saveandexit').click(function() {
passJSON();
});
/* Load the previously uploaded files */
var filecount = window.parent.window.$('#'+ia+'_filecount').val();
$('#filecount').val(filecount);
var image_extensions = new Array("png", "jpg", "jpeg", "bmp", "gif");
if (filecount > 0)
{
var jsontext = window.parent.window.$('#'+ia).val();
var json = eval('(' + jsontext + ')');
var i;
$('#licount').val(filecount);
for (i = 0; i < filecount; i++)
{
var previewblock = "<li id='li_"+i+"'><div>"+
"<table align='center'><tr>"+
"<td align='center' width='50%'>";
if (isValueInArray(image_extensions, json[i].ext))
previewblock += "<img src='upload/tmp/"+json[i].name+"' height='100px' />";
else
previewblock += "<img src='images/placeholder.png' height='100px' />";
previewblock += "</td>"+
"<td align='center'><label>Title</label><br /><br /><label>Comments</label></td>"+
"<td align='center'><input type='text' value='"+json[i].title+"' id='title_"+i+"' /><br /><br />"+
"<input type='text' value='"+json[i].comment+"' id='comment_"+i+"' /></td>"+
/* If the file is not an image, use a placeholder */
"<td align='center' width='20%'><img src='images/trash.png' onclick='deletefile("+i+")' /></td>"+
"</tr></table>"+
"<input type='hidden' id='size_"+i+"' value="+json[i].size+" />"+
"<input type='hidden' id='name_"+i+"' value="+json[i].name+" />"+
"<input type='hidden' id='ext_" +i+"' value="+json[i].ext+" />"+
"</div></li>";
// add file to the list
$('#listfiles').append(previewblock);
}
}
// The upload button
var button = $('#button1'), interval;
new AjaxUpload(button, {
action: 'upload.php',
name: 'uploadfile',
data: {
valid_extensions : $('#allowed_filetypes').val(),
maxfilesize : $('#maxfilesize').val(),
preview : $('#preview').val()
},
onSubmit : function(file, ext){
var maxfiles = $('#maxfiles').val();
var filecount = $('#filecount').val();
var allowed_filetypes = $('#allowed_filetypes').val().split(",");
/* If maximum number of allowed filetypes have already been uploaded,
* do not upload the file and display an error message ! */
if (filecount >= maxfiles)
{
$('body').prepend('<div class="notice">Sorry, No more files can be uploaded !</div>');
setTimeout(function() {
$(".notice").remove();
}, 10000);
return false;
}
/* If the file being uploaded is not allowed,
* do not upload the file and display an error message ! */
var allowSubmit = false;
for (var i = 0; i < allowed_filetypes.length; i++)
{
//check to see if it's the proper extension
if (allowed_filetypes[i] == ext)
{
//it's the proper extension
allowSubmit = true;
break;
}
}
if (allowSubmit == false)
{
$('body').prepend('<div class="notice">Sorry, Only "'+ $('#allowed_filetypes').val()+'" files can be uploaded for this question !</div>');
setTimeout(function() {
$(".notice").remove();
}, 5000);
return false;
}
// change button text, when user selects file
button.text('Uploading');
// If you want to allow uploading only 1 file at time,
// you can disable upload button
this.disable();
// Uploding -> Uploading. -> Uploading...
interval = window.setInterval(function(){
var text = button.text();
if (text.length < 13){
button.text(text + '.');
} else {
button.text('Uploading');
}
}, 200);
},
onComplete: function(file, response){
button.text('Upload');
window.clearInterval(interval);
// enable upload button
this.enable();
// Once the file has been uploaded via AJAX,
// the preview is appended to the list of files
var metadata = eval('(' + response + ')');
$('body').prepend('<div class="notice">'+metadata.msg+'</div>');
setTimeout(function() {
$(".notice").remove();
}, 2000);
var count = parseInt($('#licount').val());
var image_extensions = new Array("png", "jpg", "jpeg", "bmp", "gif");
if (metadata.success)
{
var previewblock = "<li id='li_"+count+"' class='previewblock'><div>"+
"<table align='center'><tr>"+
"<td align='center' width='50%'>";
// If the file is not an image, use a placeholder
if (isValueInArray(image_extensions, metadata.ext))
previewblock += "<img src='upload/tmp/"+metadata.name+"' height='100px' />";
else
previewblock += "<img src='images/placeholder.png' height='100px' />";
previewblock += "</td>"+
"<td align='center'><label>Title</label><br /><br /><label>Comments</label></td>"+
"<td align='center'><input type='text' value='' id='title_"+count+"' /><br /><br />"+
"<input type='text' value='' id='comment_"+count+"' /></td>"+
"<td align='center' width='20%'><img src='images/trash.png' onclick='deletefile("+count+")'/></td>"+
"</tr></table>"+
"<input type='hidden' id='size_"+count+"' value="+metadata.size+" />"+
"<input type='hidden' id='name_"+count+"' value="+metadata.name+" />"+
"<input type='hidden' id='ext_" +count+"' value="+metadata.ext+" />"+
"</div></li>";
// add file to the list
$('#listfiles').prepend(previewblock);
count++;
$('#licount').val(count);
var filecount = $('#filecount').val();
filecount++;
$('#filecount').val(filecount);
var maxfiles = $('#maxfiles').val();
if (filecount >= maxfiles) {
$('body').prepend('<div class="notice">Maximum number of files have been uploaded<br />You may Save and Exit !</div>');
setTimeout(function() {
$(".notice").remove();
}, 2000);
}
}
}
});
});
function isValueInArray(arr, val) {
inArray = false;
for (i = 0; i < arr.length; i++)
if (val == arr[i])
inArray = true;
return inArray;
}
// pass the JSON data from the iframe to the main survey page
function passJSON() {
var json = "[";
var filecount = 0;
var licount = parseInt($('#licount').val());
var i = 0;
while (i < licount)
{
if (filecount > 0)
json += ",";
if (!$("#li_"+i).is(":visible"))
{
i += 1;
}
else
{
json += '{"title":"' +$("#title_" +i).val()+'",'+
'"comment":"'+$("#comment_"+i).val()+'",'+
'"size":"' +$("#size_" +i).val()+'",'+
'"name":"' +$("#name_" +i).val()+'",'+
'"ext":"' +$("#ext_" +i).val()+'"}';
filecount += 1;
i += 1;
}
}
json += "]";
window.parent.window.copyJSON(json, filecount);
}
function deletefile(i) {
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
addNotice(xmlhttp.responseText);
}
xmlhttp.open('GET','delete.php?file='+$("#name_"+i).val(),true);
xmlhttp.send();
$("#li_"+i).hide();
var filecount = $('#filecount').val();
filecount--;
$('#filecount').val(filecount);
}
function addNotice(notice) {
$('body').prepend('<div class="notice">'+notice+'</div>');
setTimeout(function() {
$(".notice").remove();
}, 2000);
}
</script>
</head>
<body>
<input type="hidden" id="ia" value="<?php echo $_GET['ia'] ?>" />
<input type="hidden" id="minfiles" value="<?php echo $_GET['minfiles'] ?>" />
<input type="hidden" id="maxfiles" value="<?php echo $_GET['maxfiles'] ?>" />
<input type="hidden" id="maxfilesize" value="<?php echo $_GET['maxfilesize'] ?>" />
<input type="hidden" id="allowed_filetypes" value="<?php echo $_GET['allowed_filetypes'] ?>" />
<input type="hidden" id="preview" value="<?php echo $_GET['preview'] ?>" />
<input type="hidden" id="licount" value="0" />
<input type="hidden" id="filecount" value="0" />
<!-- The upload button -->
<div id="button1" class="button" align="center">Upload</div>
<p>You can upload <?php echo $_GET['allowed_filetypes']; ?> under <?php echo $_GET['maxfilesize']; ?> KB each</p>
<!-- The list of uploaded files -->
<ul id="listfiles"></ul>
<div id="saveandexit" class="button" align="center">Save and Exit</div>
</body>
</html>