File.class.php
7.74 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* 文件上传
*
* @name Util_Upload_File
* @package util/upload
* @version 1.1 (2013-06-14 17:24:52)
* @author fei.hong <fei.hong@yoho.cn>
* @since 1.0 (2012-08-14 18:46:51)
*/
class Util_Upload_File extends Util_Upload_Base implements Util_Upload_Interface
{
/**
* 验证
*
* @return Array
*/
public function check()
{
// 对file数据进行验证
if (!is_string($this->filePath) || !is_array($this->file) || !isset($this->file[$this->fileNames[0]]))
{
return $this->error(9, $this->file);
}
// 对所有不为空的文件进行验证
$check = $this->checkSysError();
if (array() !== $check['error'])
{
return $this->error($check['error'], $check);
}
// 对文件大小是否突破限制验证
$check = $this->checkSize();
if (array() !== $check['error'])
{
return $this->error(2, $check);
}
//验证文件格式
$check = $this->checkFormat();
if (array() !== $check['error'])
{
return $this->error(10, $check);
}
// 对是否是post过来的文件进行验证
if (!is_uploaded_file($this->file[$this->fileNames[0]]['tmp_name']))
{
return $this->error(8, $this->file);
}
// 获取文件的唯一MD5值
$data = array(); $md5 = null;
foreach ($this->file as $id => $file)
{
if (isset($file['tmp_name']))
{
$md5 = md5_file($file['tmp_name']);
if ($md5 !== false)
{
$data[$id] = array('md5' => $md5,);
}
}
}
return $this->error(0, $data);
}
/**
* 写入文件
*
* @return array
*/
public function write()
{
$this->initErrorWare();
$ret = array();
if ($this->useQiniu)
{
$ret = call_user_func(array($this->qiniuUploadClass, 'fileBatchUpload'),
array('file'=> $this->file, 'prefix' => $this->prefix,'filePath'=> $this->filePath));
}
else
{
$makePath = array(); $tmp = null;
foreach ($this->file as $id => $file)
{
$makePath = $this->makePath($file);
if ($this->createFolder($makePath['allpath']))
{
$tmp = $file['tmp_name']; unset($file['tmp_name']);
if (!@move_uploaded_file($tmp, $makePath['file']))
{
$this->errorWare['error'][$id] = $file;
}
else
{
chmod($makePath['file'], 0755);
$file['relaPath'] = $makePath['path'];
$ret[$id] = $file;
}
}
else //写入失败
{
return $this->error(7, $this->errorWare);
}
}
}
if(!empty($ret))
{
$this->errorWare['hit'] = $ret;
return $this->error(12, $this->errorWare);
}
else
{
return $this->error(7, $this->errorWare);
}
}
/**
* 写入文件
*
* @return array
*/
public function fastdfsWrite()
{
$this->initErrorWare();
$makePath = array(); $tmp = null;
foreach ($this->file as $id => $file)
{
$makePath = $this->makePath($file);
$tmp = $file['tmp_name']; unset($file['tmp_name']);
if (!$this->fastdfs->upload($makePath['file'], $tmp))
{
$this->errorWare['error'][$id] = $file;
}
else
{
$file['relaPath'] = $makePath['path'];
$this->errorWare['hit'][$id] = $file;
$this->setFastdfsQueue($makePath['file']);
}
}
return $this->error(12, $this->errorWare);
}
/**
* 写入单个文件
*
* @param integer $id (文件ID)
* @return void
* @since 1.1
*/
public function writeOne($id)
{
$this->initErrorWare();
if (!isset($this->file[$id]))
{
return $this->error(4, $this->errorWare);
}
$ret = array();
$file = $this->file[$id];
if ($this->useQiniu)
{
$ret = call_user_func(array($this->qiniuUploadClass, 'fileUpload'), array('file'=> $file,
'prefix' => $this->prefix,'filePath'=> $this->filePath));
}
else
{
$makePath = $this->makePath($file);
if ($this->createFolder($makePath['allpath']))
{
$tmp = $file['tmp_name']; unset($file['tmp_name']);
if (@move_uploaded_file($tmp, $makePath['file']))
{
$file['relaPath'] = $makePath['path'];
@chmod($makePath['file'], 0755);
}
$ret = $file;
}
}
if(!empty($ret))
{
$this->errorWare['hit'][$id] = $ret;
return $this->error(12, $this->errorWare);
}
else
{
$this->errorWare['hit'][$id] = $file;
return $this->error(7, $this->errorWare);
}
}
/**
* fastfs写入单个文件
*
* @param integer $id (文件ID)
* @return void
* @since 1.1
*/
public function fastdfsWriteOne($id)
{
$this->initErrorWare();
if (!isset($this->file[$id]))
{
return $this->error(4, $this->errorWare);
}
$file = $this->file[$id];
$makePath = $this->makePath($file);
$tmp = $file['tmp_name']; unset($file['tmp_name']);
if ($this->fastdfs->upload($makePath['file'], $tmp))
{
$file['relaPath'] = $makePath['path'];
$this->errorWare['hit'][$id] = $file;
$this->setFastdfsQueue($makePath['file']);
}
else
{
$this->errorWare['error'][$id] = $file;
}
return $this->error(12, $this->errorWare);
}
/**
* 移除指定的文件
*
* @ignore since 1.1 已不再使用
* @param integer $id (文件ID)
* @return void
*/
public function remove($id)
{
if (isset($this->file[$id]))
{
unset($this->file[$id]);
}
}
/**
* 创建文件夹
*
* @param string $path
* @return boolean
*/
private function createFolder($path)
{
if (!is_dir($path))
{
if(!$this->createFolder(dirname($path)))
{
return false;
}
if (!mkdir($path, 0755))
{
return false;
}
}
return true;
}
/**
* 组合新路径
*
* @param array $val
* @return array
*/
private function makePath($val)
{
$result = array();
if (is_array($val))
{
$fileInfo = pathinfo($val['name']);
if (empty($this->newFile)) // 默认
{
$datePath = date('/Y/m/d/H/');
$fileName = md5(date('H') . $val['name'] . $val['type'] . $val['size'] ) . '.' . strtolower($fileInfo['extension']);
$allPath = $this->filePath . $datePath;
$result['file'] = $allPath . $this->prefix . $fileName;
$result['allpath'] = $allPath;
$result['path'] = $datePath . $this->prefix . $fileName;
}
else // 已指定生成的文件名
{
$allFilePath = $this->filePath . strtolower($this->prefix . $this->newFile);
$newFileInfo = pathinfo($allFilePath);
$result['file'] = $allFilePath;
$result['allpath'] = $newFileInfo['dirname'];
$result['path'] = strtolower($this->prefix . $this->newFile);
}
}
return ($result);
}
/**
* 验证文件大小
*
* @return array
*/
private function checkSize()
{
$this->initErrorWare();
foreach ($this->file as $id => $file)
{
if (intval($file['size']) > intval($this->maxFileSize) || intval($file['size']) <=0 )
{
$file['error_mes'] = $this->errorMsg($file['error']);
$this->errorWare['error'][$id] = $file;
}
}
return $this->errorWare;
}
/**
* 验证系统错误
*
* @return array
*/
private function checkSysError()
{
$this->initErrorWare();
foreach ($this->file as $id => $file)
{
if (in_array($file['error'], array(1, 2, 3, 4, 6, 7,)))
{
$file['error_mes'] = $this->errorMsg($file['error']);
$this->errorWare['error'][$id] = $file;
}
}
return $this->errorWare;
}
/**
* 验证文件格式
*
* @return array
*/
private function checkFormat()
{
$this->initErrorWare();
foreach ($this->file as $id => $file)
{
if (!preg_match($this->format, $file['type']))
{
$this->errorWare['error'][$id] = $file;
}
}
return $this->errorWare;
}
}