-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.php
44 lines (44 loc) · 1.29 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
<?php
header('Content-type:application/json;charset=utf-8');
try {
if (isset($_FILES['file']['error'])) {
$error = $_FILES['file']['error'];
if (is_array($error)) {
throw new RuntimeException('Invalid parameters.');
}
switch ($error) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
if (isset($_FILES['file']['name']) && isset($_FILES['file']['tmp_name'])) {
$filepath = $_FILES['file']['name'];
if (!move_uploaded_file($_FILES['file']['tmp_name'], $filepath)) {
throw new RuntimeException('Failed to move uploaded file.');
}
// All good, send the response
echo json_encode([
'status' => 'ok',
'path' => $filepath
], JSON_PRETTY_PRINT);
} else {
throw new RuntimeException('Invalid filepath.');
}
} else {
throw new RuntimeException('Invalid parameters.');
}
} catch (RuntimeException $e) {
// Something went wrong, send the err message as JSON
http_response_code(400);
echo json_encode([
'status' => 'error',
'message' => $e->getMessage()
], JSON_PRETTY_PRINT);
}
?>