-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.php
71 lines (57 loc) · 1.63 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
<?php
/*=====================
* File Upload Handler
* By Pedram Asbaghi
/*=====================*/
require 'config.php';
$store = dirname(__FILE__) . STORAGE;
// Set Allowed Videos format
$allowFormats = array( 'mp4', 'mkv', '3gp' );
// check exists folder
if(!file_exists($store)) {
@mkdir($store);
@chmod($store, 0777) ;
}
// no time limit
set_time_limit(0);
// json header
header('Content-type:application/json;charset=utf-8');
try {
if (
!isset($_FILES['file']['error']) ||
is_array($_FILES['file']['error'])
) {
throw new RuntimeException('Invalid parameters.');
}
switch ($_FILES['file']['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.');
}
$filepath = sprintf('%s_%s', uniqid(), $_FILES['file']['name']);
// check extension
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if(!in_array($extension, $allowFormats))
{
exit(json_encode(['status' => 'Not Allowed To Upload This File !']));
}
else {
if (!move_uploaded_file(
$_FILES['file']['tmp_name'],
$store . $filepath
)) {
throw new RuntimeException('Failed to move uploaded file.');
}
}
exit(json_encode(['status' => true, 'file' => '.' . STORAGE . $filepath]));
} catch (RuntimeException $e) {
http_response_code(400);
exit(json_encode(['status' => $e->getMessage()]));
}