11
11
namespace inhere \tools \files ;
12
12
13
13
use inhere \tools \helpers \StrHelper ;
14
+ use inhere \tools \exceptions \FileSystemException ;
14
15
15
16
/**
16
17
* Class File
@@ -53,7 +54,6 @@ public static function getInfo($filename, $check=true)
53
54
'update_time ' => filectime ($ filename ), //修改时间
54
55
'last_visit_time ' => fileatime ($ filename ), //文件的上次访问时间
55
56
];
56
-
57
57
}
58
58
59
59
/**
@@ -93,8 +93,7 @@ public static function save($data, $filename )
93
93
**/
94
94
public static function createAndWrite (array $ fileData = [],$ append =false ,$ mode =0664 )
95
95
{
96
- foreach ($ fileData as $ file =>$ content )
97
- {
96
+ foreach ($ fileData as $ file =>$ content ) {
98
97
$ dir = dirname ($ file ); //文件所在目录
99
98
100
99
//检查目录是否存在,不存在就先创建(多级)目录
@@ -113,7 +112,7 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
113
112
file_put_contents ($ file ,$ content ,FILE_APPEND | LOCK_EX );
114
113
@chmod ($ file ,$ mode );
115
114
} else {
116
- \Trigger::notice ('目录 ' .$ dir .'下: ' .$ fileName ." 文件已存在!将跳过 " .$ fileName ."的创建 " ) ;
115
+ // \Trigger::notice('目录'.$dir.'下:'.$fileName." 文件已存在!将跳过".$fileName."的创建") ;
117
116
continue ;
118
117
}
119
118
@@ -125,60 +124,78 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
125
124
/**
126
125
* @param $url
127
126
* @param bool|false $use_include_path
128
- * @param null $stream_context
127
+ * @param null $streamContext
129
128
* @param int $curl_timeout
130
129
* @return bool|mixed|string
131
130
*/
132
- public static function file_get_contents ($ url , $ use_include_path = false , $ stream_context = null , $ curl_timeout = 5 )
131
+ public static function get ($ url , $ use_include_path = false , $ streamContext = null , $ curl_timeout = 5 )
133
132
{
134
- if ($ stream_context == null && preg_match ('/^https?:\/\// ' , $ url ))
135
- $ stream_context = @stream_context_create (array ('http ' => array ('timeout ' => $ curl_timeout )));
136
- if (in_array (ini_get ('allow_url_fopen ' ), array ('On ' , 'on ' , '1 ' )) || !preg_match ('/^https?:\/\// ' , $ url ))
137
- return @file_get_contents ($ url , $ use_include_path , $ stream_context );
138
- elseif (function_exists ('curl_init ' ))
139
- {
133
+ return static ::file_get_contents ($ url , $ use_include_path , $ streamContext , $ curl_timeout );
134
+ }
135
+ public static function file_get_contents ($ url , $ use_include_path = false , $ streamContext = null , $ curl_timeout = 5 )
136
+ {
137
+ if ($ streamContext == null && preg_match ('/^https?:\/\// ' , $ url )) {
138
+ $ streamContext = @stream_context_create (array ('http ' => array ('timeout ' => $ curl_timeout )));
139
+ }
140
+
141
+ if (in_array (ini_get ('allow_url_fopen ' ), array ('On ' , 'on ' , '1 ' )) || !preg_match ('/^https?:\/\// ' , $ url )) {
142
+ return @file_get_contents ($ url , $ use_include_path , $ streamContext );
143
+ } elseif (function_exists ('curl_init ' )) {
140
144
$ curl = curl_init ();
141
145
curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , 1 );
142
146
curl_setopt ($ curl , CURLOPT_URL , $ url );
143
147
curl_setopt ($ curl , CURLOPT_CONNECTTIMEOUT , 5 );
144
148
curl_setopt ($ curl , CURLOPT_TIMEOUT , $ curl_timeout );
145
149
curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , 0 );
146
- if ( $ stream_context != null )
147
- {
148
- $ opts = stream_context_get_options ($ stream_context );
149
- if ( isset ( $ opts [ ' http ' ][ ' method ' ]) && StrHelper:: strtolower ( $ opts [ ' http ' ][ ' method ' ]) == ' post ' )
150
- {
150
+
151
+ if ( $ streamContext != null ) {
152
+ $ opts = stream_context_get_options ($ streamContext );
153
+
154
+ if ( isset ( $ opts [ ' http ' ][ ' method ' ]) && StrHelper:: strtolower ( $ opts [ ' http ' ][ ' method ' ]) == ' post ' ) {
151
155
curl_setopt ($ curl , CURLOPT_POST , true );
152
- if ( isset ( $ opts [ ' http ' ][ ' content ' ]))
153
- {
156
+
157
+ if ( isset ( $ opts [ ' http ' ][ ' content ' ])) {
154
158
parse_str ($ opts ['http ' ]['content ' ], $ post_data );
155
159
curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ post_data );
156
160
}
157
161
}
158
162
}
163
+
159
164
$ content = curl_exec ($ curl );
160
165
curl_close ($ curl );
166
+
161
167
return $ content ;
162
168
}
163
- else
164
- return false ;
169
+
170
+ return false ;
165
171
}
166
172
167
- public static function move ()
168
- {}
173
+ public static function move ($ file , $ target )
174
+ {
175
+ Directory::create (dirname ($ target ));
176
+
177
+ if ( static ::copy ($ file , $ target ) ) {
178
+ unlink ($ file );
179
+ }
180
+ }
169
181
170
182
/**
171
183
* @param $source
172
184
* @param $destination
173
- * @param null $stream_context
185
+ * @param null $streamContext
174
186
* @return bool|int
175
187
*/
176
- public static function copy ($ source , $ destination , $ stream_context = null )
188
+ public static function copy ($ source , $ destination , $ streamContext = null )
177
189
{
178
- if (is_null ($ stream_context ) && !preg_match ('/^https?:\/\// ' , $ source ))
179
- return @copy ($ source , $ destination );
190
+ if (is_null ($ streamContext ) && !preg_match ('/^https?:\/\// ' , $ source )) {
191
+ if (!is_file ($ source )) {
192
+ throw new FileSystemException ("file don't exists. File: $ source " );
193
+ }
194
+
195
+ return copy ($ source , $ destination );
196
+ }
180
197
181
- return @file_put_contents ($ destination , self ::file_get_contents ($ source , false , $ stream_context ));
198
+ return @file_put_contents ($ destination , self ::file_get_contents ($ source , false , $ streamContext ));
182
199
}
183
200
184
201
public static function combine ($ inFile , $ outFile )
0 commit comments