-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.php
90 lines (81 loc) · 2.49 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
<?php
$path = __DIR__ . '/uploads';
$name = $_POST['name'];
$md5 = $_POST['md5'];
if ($_GET['type'] == 'miao') {
if (file_exists($path . '/' . $name) && md5_file($path . '/' . $name) === $md5) {
// 文件已存在,妙传
echoJson(200, 'ok', [
'url' => 'uploads/' . $name,
]);
}
} else if ($_GET['type'] == 'shard') {
$file = $_FILES['file'];
$total = $_POST['total'];
$index = $_POST['index'];
$size = $_POST['size'];
$dst_file = $path . '/' . $name . '-' . $total . ':' . $index;
if ($file["error"] > 0) {
echoJson(400, $file["error"]);
} else {
$res = move_uploaded_file($file['tmp_name'], $dst_file);
if ($res) {
file_put_contents($dst_file . '.info', $size);
echoJson(200, 'shard ok');
} else {
echoJson(400, 'shard move_uploaded_file error');
}
}
} else if ($_GET['type'] == 'merge') {
$size = $_POST['size'];
$total = $_POST['total'];
$msg = '';
if (mergeFile($path . '/' . $name, $total, $msg)) {
echoJson(200, 'ok', [
'url' => 'uploads/' . $name,
]);
} else {
echoJson(400, $msg);
}
}
function mergeFile($name, $total, &$msg)
{
for ($i = 0; $i < $total; $i++) {
if (!file_exists($name . '-' . $total . ':' . $i . '.info') || !file_exists($name . '-' . $total . ':' . $i)) {
$msg = "shard error $i";
return false;
} else if (filesize($name . '-' . $total . ':' . $i) != file_get_contents($name . '-' . $total . ':' . $i . '.info')) {
$msg = "shard size error $i";
return false;
}
}
@unlink($name);
if (file_exists($name . '.lock')) {
$msg = 'on lock';
return false;
}
touch($name . '.lock');
$file = fopen($name, 'a+');
for ($i = 0; $i < $total; $i++) {
$shardFile = fopen($name . '-' . $total . ':' . $i, 'r');
$shardData = fread($shardFile, filesize($name . '-' . $total . ':' . $i));
fwrite($file, $shardData);
fclose($shardFile);
unlink($name . '-' . $total . ':' . $i);
unlink($name . '-' . $total . ':' . $i . '.info');
}
fclose($file);
unlink($name . '.lock');
return true;
}
function echoJson($code, $msg = '', $data = [])
{
header('Content-type: application/json');
//sleep(mt_rand(1,10));
echo json_encode([
'code' => $code,
'msg' => $msg,
'data' => (object)$data
]);
exit();
}