-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpload.php
145 lines (125 loc) · 3.16 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
<?php
/**
* 上传组件
*
* @Author 陆之岇(Kraity)
* @Studio 南博网络科技工作室
* @GitHub https://github.com/krait-team
*/
class Nabo_Upload
{
/**
* @throws Exception
*/
public function launch()
{
// name
$name = $_POST['name'];
// token
$token = $_POST['token'];
// check
if (empty($name) || empty($token)) {
$this->response('缺少必要参数');
}
// report
error_reporting(E_ERROR);
include_once 'Kat/Kat.php';
include_once 'Kat/Plus.php';
include_once 'User.php';
include_once 'Format.php';
$liteuser = new Nabo_User();
$liteuser->identity([
'name' => $name,
'token' => $token
]);
try {
$liteuser->register();
} catch (Exception $e) {
$this->response(
$e->getMessage()
);
}
if (!$liteuser->pairing()) {
$this->response('权限不足');
}
try {
$this->upload();
} catch (Exception $e) {
$this->response(
$e->getMessage()
);
}
}
/**
* @target upload
*/
public function upload()
{
// check
if (!current_user_can('upload_files')) {
$this->response('无权限');
}
if (empty($_FILES)) {
$this->response('不存在文件');
}
// select
$file = array_pop($_FILES);
if ($file['error'] != 0 || !is_uploaded_file($file['tmp_name'])) {
$this->response('文件异常');
}
// check
$upload_err = apply_filters(
'pre_upload_error', false
);
if ($upload_err) {
$this->response(
(string)$upload_err
);
}
// upload
$upload = wp_upload_bits(
$file['name'], null,
file_get_contents($file['tmp_name'])
);
// check
if (!empty($upload['error'])) {
$errorString = sprintf('Could not write file %1$s (%2$s).', $file['name'], $upload['error']);
$this->response($errorString);
}
$post_id = 0;
$attachment = array(
'guid' => $upload['url'],
'post_title' => $file['name'],
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $post_id,
'post_mime_type' => $file['type'],
);
// Save the data.
$id = wp_insert_attachment(
$attachment, $upload['file'], $post_id
);
if (empty($id)) {
$this->response('文件数据入库失败');
}
$this->response(
'上传成功', 1,
Nabo_Format::media(
get_post($id)
)
);
}
/**
* @param $msg
* @param int $code
* @param null $data
*/
public function response($msg, $code = 0, $data = null)
{
kat_response([
'code' => $code,
'msg' => $msg,
'data' => $data
]);
}
}