-
Notifications
You must be signed in to change notification settings - Fork 988
/
upload.php
61 lines (55 loc) · 2.17 KB
/
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
<?php
$uploaddir = 'upload/tmp/';
$file = $uploaddir . basename($_FILES['uploadfile']['name']);
$size=$_FILES['uploadfile']['size'];
$valid_extensions = $_POST['valid_extensions'];
$maxfilesize = $_POST['maxfilesize'];
$preview = $_POST['preview'];
$valid_extensions_array = explode(",", $valid_extensions);
$pathinfo = pathinfo(basename($file));
$ext = $pathinfo['extension'];
// check to ensure that the file does not cross the maximum file size
if($size > $maxfilesize)
{
$return = array(
"success" => false,
"msg" => "Sorry, This file is too large. Only files upto ".$maxfilesize." KB are allowed"
);
unlink($_FILES['uploadfile']['tmp_name']);
echo json_encode($return);
}
// check to see that this file type is allowed
// it is also checked at the client side, but jst double checking
else if (!in_array($ext, $valid_extensions_array))
{
$return = array(
"success" => false,
"msg" => "Sorry, This file extension (".$ext.") is not allowed !".$valid_extensions
);
unlink($_FILES['uploadfile']['tmp_name']);
echo json_encode($return);
}
// if everything went fine and the file was uploaded successfuly,
// send the file related info back to the client
else if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) {
$size = filesize($file);
$return = array(
"success" => true,
"size" => $size,
"name" => basename($file),
"ext" => $ext,
"msg" => "The file has been successfuly uploaded"
);
if ($preview == 0)
unlink($_FILES['uploadfile']['tmp_name']);
echo json_encode($return);
}
// if there was some error, report error message
else {
$return = array(
"success" => false,
"msg" => "Unknown error"
);
echo json_encode($return);
}
?>