file_class.php
9.96 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
<?php
/**
* Tiny - A PHP Framework For Web Artisans
* @author Tiny <tinylofty@gmail.com>
* @copyright Copyright(c) 2010-2014 http://www.tinyrise.com All rights reserved
* @version 1.0
*/
/**
* 文件处理类
*
* @author Tiny
* @class File
*/
class File
{
private $file;
private $fileName;
/**
* 构造方法
*
* @access public
* @param mixed $file
* @param string $mode
* @return mixed
*/
public function __construct($file=null,$mode='r+')
{
if(!is_null($file)){
$this->fileName = $file;
$dir=dirname($this->fileName);
if(!file_exists($dir))$this->mkdir($dir);
$this->file = fopen($file,$mode);
}
}
/**
* 析构关闭成功
*
* @access public
* @return mixed
*/
public function __destruct()
{
if(is_resource($this->file)){
fclose($this->file);
unset($this->fileName);
unset($this->file);
}
}
/**
* 写入数据
*
* @access public
* @param mixed $str
* @return mixed
*/
public function write($str)
{
$falg = false;
if(is_writeable($this->fileName) && flock($this->file, LOCK_EX | LOCK_NB)){
if(is_object($str) || is_array($str)) $str = serialize($str);
$flag = fwrite($this->file,$str)>0?true:false;
}
unset($str);
flock($this->file, LOCK_UN | LOCK_NB);
return $flag;
}
/**
* 读取数据
*
* @access public
* @return mixed
*/
public function read()
{
$contents = fread($this->file, filesize($this->fileName));
$tem = substr($contents,0,10);
if(preg_match('/^[Oa]:\d+:.*/',$tem)){
$contents = unserialize($contents);
}
return $contents;
}
/**
* 读取指定的一行记录
*
* @access public
* @param int $start 开始位置
* @param int $len
* @return mixed
*/
public function gets($start=0,$len=1)
{
if($start<=0)$start = 0;
if($len<1) $len = 1;
$end = $start+$len;
$contents='';
for($i = 0; !feof($this->file) && $i<$end-1;$i++){
if($i >= $start-1)$contents.="line: ".($i+1)." ".fgets($this->file);
else fgets($this->file);
}
return $contents;
}
/**
* 取得文件内容
*
* @access public
* @param mixed $fileName
* @return mixed
*/
public static function getContents($fileName)
{
return file_get_contents($fileName);
}
/**
* 写入文件内容
*
* @access public
* @param mixed $fileName
* @param mixed $contents
* @return mixed
*/
public static function putContents($fileName,$contents)
{
$fileName = str_replace('/',DIRECTORY_SEPARATOR, $fileName);
if(!file_exists($fileName)) self::mkdir(dirname($fileName));
return file_put_contents($fileName,$contents);
}
/**
* 创建目录,实现多级目录的创建
*
* @access public
* @param mixed $dir
* @param int $right
* @return mixed
*/
public static function mkdir($dir,$right=0777)
{
return is_dir($dir) || (self::mkdir(dirname($dir)) && mkdir($dir, $right));
}
/**
* 删除目录下的所有文件
*
* @access public
* @param mixed $dir
* @return mixed
*/
public static function rmdir($dir)
{
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!self::rmdir($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!self::rmdir($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}
/**
* 目录复制功能
*
* @access public
* @param mixed $source 源目录
* @param mixed $dest 目标目录
* @param bool $oncemore
* @return boolean
*/
public static function xcopy($source, $dest ,$oncemore=true)
{
if (!file_exists($source)){
trigger_error("$source is not exist!",E_ERROR);
}
if (is_dir($source)){
if (file_exists($dest) && !is_dir($dest)){
trigger_error("$dest is not a dir",E_ERROR);
}
if (!file_exists($dest)){
self::mkdir($dest,0777);
}
$od = opendir($source);
while ($one = readdir($od)){
if ($one=="." || $one==".."){
continue;
}
$result = self::xcopy($source.DIRECTORY_SEPARATOR.$one, $dest.DIRECTORY_SEPARATOR.$one, $oncemore);
if ($result !== true){
return $result;
}
}
closedir($od);
}else{
if (file_exists($dest) || is_dir($dest) ){
if ( func_num_args()>2 || $oncemore===true ){
trigger_error("$dest is not a dir",E_ERROR);
}
$result = self::xcopy($source, $dest.DIRECTORY_SEPARATOR.basename($source), $oncemore);
if ( $result !== true ){
return $result;
}
}else{
if(!file_exists(dirname($dest))) self::mkdir(dirname($dest));
copy($source, $dest);
touch($dest, filemtime($source));
}
}
return true;
}
/**
* socket 功能访问
*
* @access public
* @param mixed $url
* @param int $limit
* @param string $post
* @param string $cookie
* @param string $ip
* @param int $timeout
* @param bool $block
* @return mixed
*/
public static function socket($url, $limit = 0, $post = '', $cookie = '', $ip = '', $timeout = 20, $block = TRUE)
{
$return = '';
$matches = parse_url($url);
!isset($matches['host']) && $matches['host'] = '';
!isset($matches['path']) && $matches['path'] = '';
!isset($matches['query']) && $matches['query'] = '';
!isset($matches['port']) && $matches['port'] = '';
$host = $matches['host'];
$path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
$port = !empty($matches['port']) ? $matches['port'] : 80;
if($post){
$out = "POST $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "Content-Type: application/x-www-form-urlencoded\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= 'Content-Length: '.strlen($post)."\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cache-Control: no-cache\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
$out .= $post;
}else{
$out = "GET $path HTTP/1.0\r\n";
$out .= "Accept: */*\r\n";
$out .= "Accept-Language: zh-cn\r\n";
$out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n";
$out .= "Cookie: $cookie\r\n\r\n";
}
$fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
if(!$fp){
return '';
}else{
stream_set_blocking($fp, $block);
stream_set_timeout($fp, $timeout);
@fwrite($fp, $out);
$status = stream_get_meta_data($fp);
if(!$status['timed_out']){
while (!feof($fp)){
if(($header = @fgets($fp)) && ($header == "\r\n" || $header == "\n"))break;
}
$stop = false;
while(!feof($fp) && !$stop){
$data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
$return .= $data;
if($limit){
$limit -= strlen($data);
$stop = $limit <= 0;
}
}
}
@fclose($fp);
return $return;
}
}
/**
* curl 功能
*
* @access public
* @param mixed $url
* @param array $conf
* @return mixed
*/
public static function curl_open($url, $conf = array())
{
if(!function_exists('curl_init') or !is_array($conf)) return FALSE;
$post = '';
$purl = parse_url($url);
$arr = array(
'post' => FALSE,
'return' => TRUE,
'cookie' => APP_ROOT.'data/cookie.txt',);
$arr = array_merge($arr, $conf);
$ch = curl_init();
if($purl['scheme'] == 'https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
if($arr['post'] != FALSE){
curl_setopt($ch, CURL_POST, TRUE);
if(is_array($arr['post'])){
$post = http_build_query($arr['post']);
} else {
$post = $arr['post'];
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}