-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload_file.php
executable file
·73 lines (67 loc) · 1.84 KB
/
upload_file.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
<?php
$file = $_FILES['file'];
$file_name = $file['name'];
$file_size = $file['size'] / 1024;
$file_type = strtolower(strrchr($file_name, "."));
$MAX_SIZE = 200;
$estate_id = $_POST['estate_id'];
$file_id = time();
$dst_path = "upload_files/" . $estate_id . '/';
$dst_file_name = $dst_path . $file_id . $file_type;
$type_allow = array(".jpeg", ".bmp", ".gif", ".jpg", ".png");
if (!in_array($file_type, $type_allow)) {
echo json_encode(array(
'code' => 403,
'data' => array(
'msg' => 'file type : ' . $file_type . ' not allow'
)
));
return;
}
if (!is_dir($dst_path)) {
if (!mkdir($dst_path, 0777, true)) {
echo json_encode(array(
'code' => 500,
'data' => array(
'msg' => '创建文件失败'
)
));
return;
}
}
if ($file_size < $MAX_SIZE) {
if ($file["error"] > 0) {
echo json_encode(array(
'code' => 500,
'data' => array(
'msg' => $file["error"]
)
));
} else {
$ret = move_uploaded_file($file["tmp_name"], $dst_file_name);
if ($ret) {
echo json_encode(array(
'code' => 200,
'data' => array(
'msg' => '上传成功',
'src'=>$dst_file_name,
'id'=>$file_id.$file_type
)
));
} else {
echo json_encode(array(
'code' => 500,
'data' => array(
'msg' => '上传失败'
)
));
}
}
} else {
echo json_encode(array(
'code' => 500,
'data' => array(
'msg' => '文件大小不能超过' . $MAX_SIZE . 'KB'
)
));
}