-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupload.php
161 lines (148 loc) · 4.44 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
header('Content-type:text/json'); // 规定返回的内容是Json数据
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$jaonDataName = 'files/data.json';
// 文件存储位置
$filePath = getPath();
// 从文件中读取数据到PHP变量
$json_string = file_get_contents($jaonDataName);
// 把JSON字符串转成PHP数组
$datas = json_decode($json_string, true);
// 显示出来看看
// echo var_dump($datas);
if (isset($_REQUEST['key'])) {
foreach ($datas as $key => $value) {
if (in_array($_REQUEST['key'], $value)) {
echo json_encode([
'status' => 0,
'message' => $value
]);
exit();
}
}
echo json_encode([
'status' => 1,
'message' => '文件不存在'
]);
} else {
//保存上传的文件
$return = saveFile($filePath);
//文件保存成功
if ($return['status'] == 0) {
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
// 添加新的数据
array_push($datas, array('key' => $return['key'], 'value' => $filePath . DIRECTORY_SEPARATOR . $return['name']));
// 把PHP数组转成JSON字符串
$json_string = json_encode($datas);
// 写入文件
file_put_contents($jaonDataName, $json_string);
}
// 输出到界面
echo json_encode($return);
}
}
/**
* 获取存储的文件目录
* @return string
*/
function getPath()
{
ini_set('date.timezone', 'Asia/Shanghai'); // 设置时区
$path = 'files' . DIRECTORY_SEPARATOR . date('Ymd', time());
if (!is_dir($path)) mkdir($path);
return $path;
}
/**
* 保存上传的文件到一个目录
*
* @param string $dir
* @param string $dest
* @return array
*/
function saveFile($dir, $dest = '')
{
$sum = isset($_POST['sum']) ? trim($_POST['sum']) : '';
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$index = isset($_POST['index']) ? intval($_POST['index']) : 0;
$count = isset($_POST['count']) ? intval($_POST['count']) : 0;
//禁止上传文件类型
if ((strpos($name, '.php') || strpos($name, '.html')) !== false) {
return array(
'status' => 1,
'message' => '该类型文件禁止上传'
);
}
if (empty($sum) || empty($name) || $index < 0 || $index >= $count) {
return array(
'status' => 1,
'message' => false
);
}
if ($_FILES['data']['error'] > 0) {
return array(
'status' => $_FILES['data']['error'],
'message' => getErrorMessage($_FILES['data']['error'])
);
}
$dest = empty($dest) ? $name : $dest;
$dest = $dir . DIRECTORY_SEPARATOR . $dest;
if (file_exists($dest)) {
return array(
'status' => 2,
'message' => '同名文件已经存在'
);
}
copy($_FILES['data']['tmp_name'], sys_get_temp_dir() . DIRECTORY_SEPARATOR . $sum . '-' . $index);
if ($index + 1 == $count) {
$fd = fopen($dest, 'x');
if (false === $fd && !flock($fd, LOCK_EX)) {
return array(
'status' => 1,
'message' => '打开文件失败'
);
}
for ($i = 0; $i < $count; $i++) {
$tmp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $sum . '-' . $i;
fwrite($fd, file_get_contents($tmp));
unlink($tmp);
}
flock($fd, LOCK_UN);
fclose($fd);
}
return array(
'name' => $name,
'key' => md5_file($dest),
'status' => 0,
'message' => $index + 1 == $count
);
}
/**
* 根据错误代码获取错误信息
*
* @param int $code
* @return string
*/
function getErrorMessage($code)
{
switch ($code) {
case UPLOAD_ERR_OK:
case UPLOAD_ERR_FORM_SIZE:
return '文件块太大';
break;
case UPLOAD_ERR_PARTIAL:
return '文件没有完整上传';
break;
case UPLOAD_ERR_NO_FILE:
return '文件没有上传';
break;
case UPLOAD_ERR_NO_TMP_DIR:
return '找不到临时文件夹';
break;
case UPLOAD_ERR_CANT_WRITE:
return '文件写入失败';
break;
default:
return '未知错误';
break;
}
}