-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathupload.php
77 lines (65 loc) · 2.22 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
$postFileId = "file";
if ($_FILES[$postFileId]["name"] == NULL)
{
header("HTTP/1.0 404 Not Found");
exit;
}
$response['status'] = 'success';
$response['message'] = '';
$imagesDir = "images/";
$uploadsDir = "uploads/";
$target_dir = dirname(__FILE__) . '/' . $imagesDir . $uploadsDir;
//$response['targetDir'] .= $target_dir;
if (!file_exists($target_dir))
{
//if (!mkdir($target_dir, 0777, true))
$response['message'] .= $target_dir ." DOES NOT EXIST";
$uploadOk = 0;
}
$uploadOk = 1;
$target_file = $target_dir;
//prefix with "icon_"
if (stripos($_FILES[$postFileId]["name"], "icon_") != 0)
$target_file .= basename($_FILES[$postFileId]["name"]);
else
$target_file .= "icon_" . basename($_FILES[$postFileId]["name"]);
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES[$postFileId]["tmp_name"]);
if($check !== false) {
$response['message'] .= "File is an image - " . $check["mime"];
//$uploadOk = 1;
} else {
$response['message'] .= "File is not an image ";
$uploadOk = 0;
}
}
// Check if file already exists (delete old one, overwrite below)
if (file_exists($target_file)) unlink($target_file);
// Check file size
if ($_FILES[$postFileId]["size"] > 500000) {
$response['message'] .= "File too large (>500K) ";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
$response['message'] .= "Only JPG/JPEG/PNG/GIF are allowed ";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$response['status'] = 'error';
$response['message'] .= "Upload FAILED";
} else { //all ok, try to upload file
if (move_uploaded_file($_FILES[$postFileId]["tmp_name"], $target_file)) {
$response['message'] = "Upload OK";
$response['filePath'] = $uploadsDir . basename($target_file);
} else {
$response['status'] = 'error';
$response['message'] .= "Upload FAILED";
}
}
echo json_encode($response);
?>