1111namespace inhere \tools \files ;
1212
1313use inhere \tools \helpers \StrHelper ;
14+ use inhere \tools \exceptions \FileSystemException ;
1415
1516/**
1617 * Class File
@@ -53,7 +54,6 @@ public static function getInfo($filename, $check=true)
5354 'update_time ' => filectime ($ filename ), //修改时间
5455 'last_visit_time ' => fileatime ($ filename ), //文件的上次访问时间
5556 ];
56-
5757 }
5858
5959 /**
@@ -93,8 +93,7 @@ public static function save($data, $filename )
9393 **/
9494 public static function createAndWrite (array $ fileData = [],$ append =false ,$ mode =0664 )
9595 {
96- foreach ($ fileData as $ file =>$ content )
97- {
96+ foreach ($ fileData as $ file =>$ content ) {
9897 $ dir = dirname ($ file ); //文件所在目录
9998
10099 //检查目录是否存在,不存在就先创建(多级)目录
@@ -113,7 +112,7 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
113112 file_put_contents ($ file ,$ content ,FILE_APPEND | LOCK_EX );
114113 @chmod ($ file ,$ mode );
115114 } else {
116- \Trigger::notice ('目录 ' .$ dir .'下: ' .$ fileName ." 文件已存在!将跳过 " .$ fileName ."的创建 " ) ;
115+ // \Trigger::notice('目录'.$dir.'下:'.$fileName." 文件已存在!将跳过".$fileName."的创建") ;
117116 continue ;
118117 }
119118
@@ -125,60 +124,78 @@ public static function createAndWrite(array $fileData = [],$append=false,$mode=0
125124 /**
126125 * @param $url
127126 * @param bool|false $use_include_path
128- * @param null $stream_context
127+ * @param null $streamContext
129128 * @param int $curl_timeout
130129 * @return bool|mixed|string
131130 */
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 )
133132 {
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 ' )) {
140144 $ curl = curl_init ();
141145 curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , 1 );
142146 curl_setopt ($ curl , CURLOPT_URL , $ url );
143147 curl_setopt ($ curl , CURLOPT_CONNECTTIMEOUT , 5 );
144148 curl_setopt ($ curl , CURLOPT_TIMEOUT , $ curl_timeout );
145149 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 ' ) {
151155 curl_setopt ($ curl , CURLOPT_POST , true );
152- if ( isset ( $ opts [ ' http ' ][ ' content ' ]))
153- {
156+
157+ if ( isset ( $ opts [ ' http ' ][ ' content ' ])) {
154158 parse_str ($ opts ['http ' ]['content ' ], $ post_data );
155159 curl_setopt ($ curl , CURLOPT_POSTFIELDS , $ post_data );
156160 }
157161 }
158162 }
163+
159164 $ content = curl_exec ($ curl );
160165 curl_close ($ curl );
166+
161167 return $ content ;
162168 }
163- else
164- return false ;
169+
170+ return false ;
165171 }
166172
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+ }
169181
170182 /**
171183 * @param $source
172184 * @param $destination
173- * @param null $stream_context
185+ * @param null $streamContext
174186 * @return bool|int
175187 */
176- public static function copy ($ source , $ destination , $ stream_context = null )
188+ public static function copy ($ source , $ destination , $ streamContext = null )
177189 {
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+ }
180197
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 ));
182199 }
183200
184201 public static function combine ($ inFile , $ outFile )
0 commit comments