-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.php
101 lines (91 loc) · 3.15 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
<?php
/**
* Created by PhpStorm.
* User: fthvg
* Date: 2017/5/17
* Time: 16:56
*/
/**
* 临时文件目录
* @return string
*/
function getPath()
{
$path = __DIR__ . '/' . date('Ym');
if (!is_dir($path)) {
mkdir($path);
}
return $path;
}
$md5 = $_POST['md5'];
$post = $_POST;
//todo 查询是否已存在md5
$file_path = getPath() . '/' . $_POST['file_name'];
if (!file_exists($file_path)) {
if (isset($post['last_upload']) && $post['last_upload'] == 'yes') {
move_uploaded_file($_FILES['file']['tmp_name'], $file_path);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file_path);
finfo_close($finfo);
/*if($mimetype!='application/java-archive'){
unlink($file_path);
echo json_encode(['code'=>0,'message'=>'类型错误']);
}*/
$md5 = md5_file($file_path);
$sha1 = sha1_file($file_path);
if ($post['md5'] == $md5) {
$data['md5'] = $md5;
$data['sha1'] = $sha1;
$data['create_time'] = time();
$data['name'] = $_POST['name'];
$data['size'] = filesize($file_path);
$save = $file_path;//实际要保存的路径
if (true) {
//todo 保存到数据库中
if ($file_path != $save) {
rename($file_path, $save);
}
echo json_encode(['code' => 1, 'fileInfo' => $data]);
} else {
unlink($file_path);
echo json_encode(['code' => 0, 'message' => '数据库保存失败']);
}
}
} else {
move_uploaded_file($_FILES['file']['tmp_name'], $file_path);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mimetype = finfo_file($finfo, $file_path);
finfo_close($finfo);
/*if($mimetype!='application/java-archive'){
unlink($file_path);
echo json_encode( ['code'=>0,'message'=>'类型出错']);
}*/
}
} else {
file_put_contents($file_path, file_get_contents($_FILES['file']['tmp_name']), FILE_APPEND);
if (isset($post['last_upload']) && $post['last_upload'] == 'yes') {
$md5 = md5_file($file_path);
$sha1 = sha1_file($file_path);
if ($post['md5'] == $md5) {
$data['md5'] = $md5;
$data['sha1'] = $sha1;
$data['create_time'] = time();
$data['name'] = $_POST['file_name'];
$data['size'] = filesize($file_path);
$save = $file_path;//实际要保存的路径
if (true) {
//todo 保存到数据库中
if ($file_path != $save) {
rename($file_path, $save);
}
echo json_encode(['code' => 1, 'fileInfo' => $data]);
} else {
unlink($file_path);
echo json_encode(['code' => 0, 'message' => '数据库保存失败']);
}
} else {
unlink($file_path);
echo json_encode(['code' => 0, 'message' => '可能是网络原因导致,md5校检不正确,请刷新后重新上传!', 'md5' => $md5]);
}
}
}