FileStream.class.php
3.75 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
<?php
/**
* 文件流上传
*
* @name Util_Upload_FileStream
* @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_FileStream extends Util_Upload_Base implements Util_Upload_Interface
{
/**
* 验证
*
* @return Array
*/
public function check()
{
// 对file数据进行验证
if (!is_string($this->filePath) || empty($this->file))
{
return $this->error(9, $this->file);
}
// 对文件大小是否突破限制验证
$check = $this->checkSize();
if (array() !== $check['error'])
{
return $this->error(2, $check);
}
return $this->error(0, array(0 => array('md5' => md5($this->file)),));
}
/**
* 写入文件
*
* @return array
*/
public function write()
{
if ($this->useFastdfs)
{
return $this->fastdfsWrite();
}
$this->initErrorWare();
if (is_string($this->file))
{
$makePath = $this->makePath();
if (false === $this->createFolder($makePath['allpath']))
{
return $this->error(7, $this->errorWare);
}
file_put_contents($makePath['file'], $this->file);
$this->errorWare['hit'][0]['relaPath'] = $makePath['path'];
@chmod($makePath['file'], 0755);
}
return $this->error(12, $this->errorWare);
}
/**
* fastdfs写入文件
*
* @return array
*/
public function fastdfsWrite()
{
$this->initErrorWare();
if (is_string($this->file))
{
$makePath = $this->makePath();
if (!$this->fastdfs->uploadByStream($makePath['file'], $this->file))
{
$this->errorWare['error'][$id] = 'error stream';
}
else
{
$this->errorWare['hit'][0]['relaPath'] = $makePath['path'];
$this->setFastdfsQueue($makePath['file']);
}
}
return $this->error(12, $this->errorWare);
}
/**
* 写入单个文件
*
* @param integer $id (文件ID)
* @return array
* @since 1.1
*/
public function writeOne($id)
{
if ($this->useFastdfs)
{
return $this->fastdfsWrite();
}
else
{
return $this->write();
}
}
/**
* 移除指定的文件
*
* @ignore since 1.1 已不再使用
* @param integer $id (文件ID)
* @return void
*/
public function remove($id)
{
if (isset($this->file[$id]))
{
unset($this->file[$id]);
}
else
{
unset($this->file);
}
}
/**
* 组合新路径
*
* @return array
*/
private function makePath()
{
if (empty($this->newFile)) // 默认
{
$datePath = date('/Y/m/d/H/');
$fileName = md5(microtime()) . '.jpg'; // 文件名以时间md5值命名
$allPath = $this->filePath . $datePath;
$allFilePath = $allPath . $this->prefix . $fileName;
$newPath = $datePath . $this->prefix . $fileName;
}
else // 已指定生成的文件名
{
$allFilePath = $this->filePath . strtolower($this->prefix . $this->newFile);
$newFileInfo = pathinfo($allFilePath);
$allPath = $newFileInfo['dirname'] . '/';
$newPath = strtolower($this->prefix . $this->newFile);
}
return array('file' => $allFilePath, 'allpath' => $allPath, 'path' => $newPath );
}
/**
* 创建文件夹
*
* @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;
}
/**
* 验证文件大小
*
* @return array
*/
private function checkSize()
{
$this->initErrorWare();
if (strlen($this->file) > $this->maxFileSize || strlen($this->file) <=0 )
{
$val['error_mes'] = $this->errorMsg(2);
$this->errorWare['error'][0] = $val;
}
return $this->errorWare;
}
}