71 |
76 |
72 reset_mbstring_encoding(); |
77 reset_mbstring_encoding(); |
73 |
78 |
74 fclose( $fp ); |
79 fclose( $fp ); |
75 |
80 |
76 if ( $data_length !== $bytes_written ) |
81 if ( $data_length !== $bytes_written ) { |
77 return false; |
82 return false; |
|
83 } |
78 |
84 |
79 $this->chmod( $file, $mode ); |
85 $this->chmod( $file, $mode ); |
80 |
86 |
81 return true; |
87 return true; |
82 } |
88 } |
83 |
89 |
84 /** |
90 /** |
85 * Gets the current working directory |
91 * Gets the current working directory. |
86 * |
92 * |
87 * |
93 * @since 2.5.0 |
88 * @return string|bool the current working directory on success, or false on failure. |
94 * |
|
95 * @return string|false The current working directory on success, false on failure. |
89 */ |
96 */ |
90 public function cwd() { |
97 public function cwd() { |
91 return @getcwd(); |
98 return @getcwd(); |
92 } |
99 } |
93 |
100 |
94 /** |
101 /** |
95 * Change directory |
102 * Changes current directory. |
96 * |
103 * |
|
104 * @since 2.5.0 |
97 * |
105 * |
98 * @param string $dir The new current directory. |
106 * @param string $dir The new current directory. |
99 * @return bool Returns true on success or false on failure. |
107 * @return bool True on success, false on failure. |
100 */ |
108 */ |
101 public function chdir($dir) { |
109 public function chdir( $dir ) { |
102 return @chdir($dir); |
110 return @chdir( $dir ); |
103 } |
111 } |
104 |
112 |
105 /** |
113 /** |
106 * Changes file group |
114 * Changes the file group. |
107 * |
115 * |
108 * |
116 * @since 2.5.0 |
109 * @param string $file Path to the file. |
117 * |
110 * @param mixed $group A group name or number. |
118 * @param string $file Path to the file. |
111 * @param bool $recursive Optional. If set True changes file group recursively. Default false. |
119 * @param string|int $group A group name or number. |
112 * @return bool Returns true on success or false on failure. |
120 * @param bool $recursive Optional. If set to true, changes file group recursively. |
113 */ |
121 * Default false. |
114 public function chgrp($file, $group, $recursive = false) { |
122 * @return bool True on success, false on failure. |
115 if ( ! $this->exists($file) ) |
123 */ |
116 return false; |
124 public function chgrp( $file, $group, $recursive = false ) { |
117 if ( ! $recursive ) |
125 if ( ! $this->exists( $file ) ) { |
118 return @chgrp($file, $group); |
126 return false; |
119 if ( ! $this->is_dir($file) ) |
127 } |
120 return @chgrp($file, $group); |
128 if ( ! $recursive ) { |
|
129 return @chgrp( $file, $group ); |
|
130 } |
|
131 if ( ! $this->is_dir( $file ) ) { |
|
132 return @chgrp( $file, $group ); |
|
133 } |
121 // Is a directory, and we want recursive |
134 // Is a directory, and we want recursive |
122 $file = trailingslashit($file); |
135 $file = trailingslashit( $file ); |
123 $filelist = $this->dirlist($file); |
136 $filelist = $this->dirlist( $file ); |
124 foreach ($filelist as $filename) |
137 foreach ( $filelist as $filename ) { |
125 $this->chgrp($file . $filename, $group, $recursive); |
138 $this->chgrp( $file . $filename, $group, $recursive ); |
|
139 } |
126 |
140 |
127 return true; |
141 return true; |
128 } |
142 } |
129 |
143 |
130 /** |
144 /** |
131 * Changes filesystem permissions |
145 * Changes filesystem permissions. |
132 * |
146 * |
133 * |
147 * @since 2.5.0 |
134 * @param string $file Path to the file. |
148 * |
135 * @param int $mode Optional. The permissions as octal number, usually 0644 for files, |
149 * @param string $file Path to the file. |
136 * 0755 for dirs. Default false. |
150 * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
137 * @param bool $recursive Optional. If set True changes file group recursively. Default false. |
151 * 0755 for directories. Default false. |
138 * @return bool Returns true on success or false on failure. |
152 * @param bool $recursive Optional. If set to true, changes file group recursively. |
139 */ |
153 * Default false. |
140 public function chmod($file, $mode = false, $recursive = false) { |
154 * @return bool True on success, false on failure. |
|
155 */ |
|
156 public function chmod( $file, $mode = false, $recursive = false ) { |
141 if ( ! $mode ) { |
157 if ( ! $mode ) { |
142 if ( $this->is_file($file) ) |
158 if ( $this->is_file( $file ) ) { |
143 $mode = FS_CHMOD_FILE; |
159 $mode = FS_CHMOD_FILE; |
144 elseif ( $this->is_dir($file) ) |
160 } elseif ( $this->is_dir( $file ) ) { |
145 $mode = FS_CHMOD_DIR; |
161 $mode = FS_CHMOD_DIR; |
146 else |
162 } else { |
147 return false; |
163 return false; |
148 } |
164 } |
149 |
165 } |
150 if ( ! $recursive || ! $this->is_dir($file) ) |
166 |
151 return @chmod($file, $mode); |
167 if ( ! $recursive || ! $this->is_dir( $file ) ) { |
|
168 return @chmod( $file, $mode ); |
|
169 } |
152 // Is a directory, and we want recursive |
170 // Is a directory, and we want recursive |
153 $file = trailingslashit($file); |
171 $file = trailingslashit( $file ); |
154 $filelist = $this->dirlist($file); |
172 $filelist = $this->dirlist( $file ); |
155 foreach ( (array)$filelist as $filename => $filemeta) |
173 foreach ( (array) $filelist as $filename => $filemeta ) { |
156 $this->chmod($file . $filename, $mode, $recursive); |
174 $this->chmod( $file . $filename, $mode, $recursive ); |
|
175 } |
157 |
176 |
158 return true; |
177 return true; |
159 } |
178 } |
160 |
179 |
161 /** |
180 /** |
162 * Changes file owner |
181 * Changes the owner of a file or directory. |
163 * |
182 * |
164 * |
183 * @since 2.5.0 |
165 * @param string $file Path to the file. |
184 * |
166 * @param mixed $owner A user name or number. |
185 * @param string $file Path to the file or directory. |
167 * @param bool $recursive Optional. If set True changes file owner recursively. |
186 * @param string|int $owner A user name or number. |
168 * Default false. |
187 * @param bool $recursive Optional. If set to true, changes file owner recursively. |
169 * @return bool Returns true on success or false on failure. |
188 * Default false. |
170 */ |
189 * @return bool True on success, false on failure. |
171 public function chown($file, $owner, $recursive = false) { |
190 */ |
172 if ( ! $this->exists($file) ) |
191 public function chown( $file, $owner, $recursive = false ) { |
173 return false; |
192 if ( ! $this->exists( $file ) ) { |
174 if ( ! $recursive ) |
193 return false; |
175 return @chown($file, $owner); |
194 } |
176 if ( ! $this->is_dir($file) ) |
195 if ( ! $recursive ) { |
177 return @chown($file, $owner); |
196 return @chown( $file, $owner ); |
|
197 } |
|
198 if ( ! $this->is_dir( $file ) ) { |
|
199 return @chown( $file, $owner ); |
|
200 } |
178 // Is a directory, and we want recursive |
201 // Is a directory, and we want recursive |
179 $filelist = $this->dirlist($file); |
202 $filelist = $this->dirlist( $file ); |
180 foreach ($filelist as $filename) { |
203 foreach ( $filelist as $filename ) { |
181 $this->chown($file . '/' . $filename, $owner, $recursive); |
204 $this->chown( $file . '/' . $filename, $owner, $recursive ); |
182 } |
205 } |
183 return true; |
206 return true; |
184 } |
207 } |
185 |
208 |
186 /** |
209 /** |
187 * Gets file owner |
210 * Gets the file owner. |
188 * |
211 * |
|
212 * @since 2.5.0 |
189 * |
213 * |
190 * @param string $file Path to the file. |
214 * @param string $file Path to the file. |
191 * @return string|bool Username of the user or false on error. |
215 * @return string|false Username of the owner on success, false on failure. |
192 */ |
216 */ |
193 public function owner($file) { |
217 public function owner( $file ) { |
194 $owneruid = @fileowner($file); |
218 $owneruid = @fileowner( $file ); |
195 if ( ! $owneruid ) |
219 if ( ! $owneruid ) { |
196 return false; |
220 return false; |
197 if ( ! function_exists('posix_getpwuid') ) |
221 } |
|
222 if ( ! function_exists( 'posix_getpwuid' ) ) { |
198 return $owneruid; |
223 return $owneruid; |
199 $ownerarray = posix_getpwuid($owneruid); |
224 } |
|
225 $ownerarray = posix_getpwuid( $owneruid ); |
200 return $ownerarray['name']; |
226 return $ownerarray['name']; |
201 } |
227 } |
202 |
228 |
203 /** |
229 /** |
204 * Gets file permissions |
230 * Gets the permissions of the specified file or filepath in their octal format. |
205 * |
231 * |
206 * FIXME does not handle errors in fileperms() |
232 * FIXME does not handle errors in fileperms() |
207 * |
233 * |
|
234 * @since 2.5.0 |
208 * |
235 * |
209 * @param string $file Path to the file. |
236 * @param string $file Path to the file. |
210 * @return string Mode of the file (last 3 digits). |
237 * @return string Mode of the file (the last 3 digits). |
211 */ |
238 */ |
212 public function getchmod($file) { |
239 public function getchmod( $file ) { |
213 return substr( decoct( @fileperms( $file ) ), -3 ); |
240 return substr( decoct( @fileperms( $file ) ), -3 ); |
214 } |
241 } |
215 |
242 |
216 /** |
243 /** |
217 * |
244 * Gets the file's group. |
218 * @param string $file |
245 * |
219 * @return string|false |
246 * @since 2.5.0 |
220 */ |
247 * |
221 public function group($file) { |
248 * @param string $file Path to the file. |
222 $gid = @filegroup($file); |
249 * @return string|false The group on success, false on failure. |
223 if ( ! $gid ) |
250 */ |
224 return false; |
251 public function group( $file ) { |
225 if ( ! function_exists('posix_getgrgid') ) |
252 $gid = @filegroup( $file ); |
|
253 if ( ! $gid ) { |
|
254 return false; |
|
255 } |
|
256 if ( ! function_exists( 'posix_getgrgid' ) ) { |
226 return $gid; |
257 return $gid; |
227 $grouparray = posix_getgrgid($gid); |
258 } |
|
259 $grouparray = posix_getgrgid( $gid ); |
228 return $grouparray['name']; |
260 return $grouparray['name']; |
229 } |
261 } |
230 |
262 |
231 /** |
263 /** |
232 * |
264 * Copies a file. |
233 * @param string $source |
265 * |
234 * @param string $destination |
266 * @since 2.5.0 |
235 * @param bool $overwrite |
267 * |
236 * @param int $mode |
268 * @param string $source Path to the source file. |
237 * @return bool |
269 * @param string $destination Path to the destination file. |
238 */ |
270 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
239 public function copy($source, $destination, $overwrite = false, $mode = false) { |
271 * Default false. |
240 if ( ! $overwrite && $this->exists($destination) ) |
272 * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
241 return false; |
273 * 0755 for dirs. Default false. |
242 |
274 * @return bool True on success, false on failure. |
243 $rtval = copy($source, $destination); |
275 */ |
244 if ( $mode ) |
276 public function copy( $source, $destination, $overwrite = false, $mode = false ) { |
245 $this->chmod($destination, $mode); |
277 if ( ! $overwrite && $this->exists( $destination ) ) { |
|
278 return false; |
|
279 } |
|
280 |
|
281 $rtval = copy( $source, $destination ); |
|
282 if ( $mode ) { |
|
283 $this->chmod( $destination, $mode ); |
|
284 } |
246 return $rtval; |
285 return $rtval; |
247 } |
286 } |
248 |
287 |
249 /** |
288 /** |
250 * |
289 * Moves a file. |
251 * @param string $source |
290 * |
252 * @param string $destination |
291 * @since 2.5.0 |
253 * @param bool $overwrite |
292 * |
254 * @return bool |
293 * @param string $source Path to the source file. |
255 */ |
294 * @param string $destination Path to the destination file. |
256 public function move($source, $destination, $overwrite = false) { |
295 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
257 if ( ! $overwrite && $this->exists($destination) ) |
296 * Default false. |
258 return false; |
297 * @return bool True on success, false on failure. |
|
298 */ |
|
299 public function move( $source, $destination, $overwrite = false ) { |
|
300 if ( ! $overwrite && $this->exists( $destination ) ) { |
|
301 return false; |
|
302 } |
259 |
303 |
260 // Try using rename first. if that fails (for example, source is read only) try copy. |
304 // Try using rename first. if that fails (for example, source is read only) try copy. |
261 if ( @rename($source, $destination) ) |
305 if ( @rename( $source, $destination ) ) { |
262 return true; |
306 return true; |
263 |
307 } |
264 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) { |
308 |
265 $this->delete($source); |
309 if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) { |
|
310 $this->delete( $source ); |
266 return true; |
311 return true; |
267 } else { |
312 } else { |
268 return false; |
313 return false; |
269 } |
314 } |
270 } |
315 } |
271 |
316 |
272 /** |
317 /** |
273 * |
318 * Deletes a file or directory. |
274 * @param string $file |
319 * |
275 * @param bool $recursive |
320 * @since 2.5.0 |
276 * @param string $type |
321 * |
277 * @return bool |
322 * @param string $file Path to the file or directory. |
278 */ |
323 * @param bool $recursive Optional. If set to true, changes file group recursively. |
279 public function delete($file, $recursive = false, $type = false) { |
324 * Default false. |
280 if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. |
325 * @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
281 return false; |
326 * Default false. |
|
327 * @return bool True on success, false on failure. |
|
328 */ |
|
329 public function delete( $file, $recursive = false, $type = false ) { |
|
330 if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. |
|
331 return false; |
|
332 } |
282 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise |
333 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise |
283 |
334 |
284 if ( 'f' == $type || $this->is_file($file) ) |
335 if ( 'f' == $type || $this->is_file( $file ) ) { |
285 return @unlink($file); |
336 return @unlink( $file ); |
286 if ( ! $recursive && $this->is_dir($file) ) |
337 } |
287 return @rmdir($file); |
338 if ( ! $recursive && $this->is_dir( $file ) ) { |
|
339 return @rmdir( $file ); |
|
340 } |
288 |
341 |
289 // At this point it's a folder, and we're in recursive mode |
342 // At this point it's a folder, and we're in recursive mode |
290 $file = trailingslashit($file); |
343 $file = trailingslashit( $file ); |
291 $filelist = $this->dirlist($file, true); |
344 $filelist = $this->dirlist( $file, true ); |
292 |
345 |
293 $retval = true; |
346 $retval = true; |
294 if ( is_array( $filelist ) ) { |
347 if ( is_array( $filelist ) ) { |
295 foreach ( $filelist as $filename => $fileinfo ) { |
348 foreach ( $filelist as $filename => $fileinfo ) { |
296 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) ) |
349 if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { |
297 $retval = false; |
350 $retval = false; |
|
351 } |
298 } |
352 } |
299 } |
353 } |
300 |
354 |
301 if ( file_exists($file) && ! @rmdir($file) ) |
355 if ( file_exists( $file ) && ! @rmdir( $file ) ) { |
302 $retval = false; |
356 $retval = false; |
|
357 } |
303 |
358 |
304 return $retval; |
359 return $retval; |
305 } |
360 } |
306 /** |
361 |
307 * |
362 /** |
308 * @param string $file |
363 * Checks if a file or directory exists. |
309 * @return bool |
364 * |
310 */ |
365 * @since 2.5.0 |
311 public function exists($file) { |
366 * |
312 return @file_exists($file); |
367 * @param string $file Path to file or directory. |
313 } |
368 * @return bool Whether $file exists or not. |
314 /** |
369 */ |
315 * |
370 public function exists( $file ) { |
316 * @param string $file |
371 return @file_exists( $file ); |
317 * @return bool |
372 } |
318 */ |
373 |
319 public function is_file($file) { |
374 /** |
320 return @is_file($file); |
375 * Checks if resource is a file. |
321 } |
376 * |
322 /** |
377 * @since 2.5.0 |
323 * |
378 * |
324 * @param string $path |
379 * @param string $file File path. |
325 * @return bool |
380 * @return bool Whether $file is a file. |
326 */ |
381 */ |
327 public function is_dir($path) { |
382 public function is_file( $file ) { |
328 return @is_dir($path); |
383 return @is_file( $file ); |
329 } |
384 } |
330 |
385 |
331 /** |
386 /** |
332 * |
387 * Checks if resource is a directory. |
333 * @param string $file |
388 * |
334 * @return bool |
389 * @since 2.5.0 |
335 */ |
390 * |
336 public function is_readable($file) { |
391 * @param string $path Directory path. |
337 return @is_readable($file); |
392 * @return bool Whether $path is a directory. |
338 } |
393 */ |
339 |
394 public function is_dir( $path ) { |
340 /** |
395 return @is_dir( $path ); |
341 * |
396 } |
342 * @param string $file |
397 |
343 * @return bool |
398 /** |
344 */ |
399 * Checks if a file is readable. |
345 public function is_writable($file) { |
400 * |
346 return @is_writable($file); |
401 * @since 2.5.0 |
347 } |
402 * |
348 |
403 * @param string $file Path to file. |
349 /** |
404 * @return bool Whether $file is readable. |
350 * |
405 */ |
351 * @param string $file |
406 public function is_readable( $file ) { |
352 * @return int |
407 return @is_readable( $file ); |
353 */ |
408 } |
354 public function atime($file) { |
409 |
355 return @fileatime($file); |
410 /** |
356 } |
411 * Checks if a file or directory is writable. |
357 |
412 * |
358 /** |
413 * @since 2.5.0 |
359 * |
414 * |
360 * @param string $file |
415 * @param string $file Path to file or directory. |
361 * @return int |
416 * @return bool Whether $file is writable. |
362 */ |
417 */ |
363 public function mtime($file) { |
418 public function is_writable( $file ) { |
364 return @filemtime($file); |
419 return @is_writable( $file ); |
365 } |
420 } |
366 |
421 |
367 /** |
422 /** |
368 * |
423 * Gets the file's last access time. |
369 * @param string $file |
424 * |
370 * @return int |
425 * @since 2.5.0 |
371 */ |
426 * |
372 public function size($file) { |
427 * @param string $file Path to file. |
373 return @filesize($file); |
428 * @return int|false Unix timestamp representing last access time, false on failure. |
374 } |
429 */ |
375 |
430 public function atime( $file ) { |
376 /** |
431 return @fileatime( $file ); |
377 * |
432 } |
378 * @param string $file |
433 |
379 * @param int $time |
434 /** |
380 * @param int $atime |
435 * Gets the file modification time. |
381 * @return bool |
436 * |
382 */ |
437 * @since 2.5.0 |
383 public function touch($file, $time = 0, $atime = 0) { |
438 * |
384 if ($time == 0) |
439 * @param string $file Path to file. |
|
440 * @return int|false Unix timestamp representing modification time, false on failure. |
|
441 */ |
|
442 public function mtime( $file ) { |
|
443 return @filemtime( $file ); |
|
444 } |
|
445 |
|
446 /** |
|
447 * Gets the file size (in bytes). |
|
448 * |
|
449 * @since 2.5.0 |
|
450 * |
|
451 * @param string $file Path to file. |
|
452 * @return int|false Size of the file in bytes on success, false on failure. |
|
453 */ |
|
454 public function size( $file ) { |
|
455 return @filesize( $file ); |
|
456 } |
|
457 |
|
458 /** |
|
459 * Sets the access and modification times of a file. |
|
460 * |
|
461 * Note: If $file doesn't exist, it will be created. |
|
462 * |
|
463 * @since 2.5.0 |
|
464 * |
|
465 * @param string $file Path to file. |
|
466 * @param int $time Optional. Modified time to set for file. |
|
467 * Default 0. |
|
468 * @param int $atime Optional. Access time to set for file. |
|
469 * Default 0. |
|
470 * @return bool True on success, false on failure. |
|
471 */ |
|
472 public function touch( $file, $time = 0, $atime = 0 ) { |
|
473 if ( $time == 0 ) { |
385 $time = time(); |
474 $time = time(); |
386 if ($atime == 0) |
475 } |
|
476 if ( $atime == 0 ) { |
387 $atime = time(); |
477 $atime = time(); |
388 return @touch($file, $time, $atime); |
478 } |
389 } |
479 return @touch( $file, $time, $atime ); |
390 |
480 } |
391 /** |
481 |
392 * |
482 /** |
393 * @param string $path |
483 * Creates a directory. |
394 * @param mixed $chmod |
484 * |
395 * @param mixed $chown |
485 * @since 2.5.0 |
396 * @param mixed $chgrp |
486 * |
397 * @return bool |
487 * @param string $path Path for new directory. |
398 */ |
488 * @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod). |
399 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { |
489 * Default false. |
|
490 * @param string|int $chown Optional. A user name or number (or false to skip chown). |
|
491 * Default false. |
|
492 * @param string|int $chgrp Optional. A group name or number (or false to skip chgrp). |
|
493 * Default false. |
|
494 * @return bool True on success, false on failure. |
|
495 */ |
|
496 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
400 // Safe mode fails with a trailing slash under certain PHP versions. |
497 // Safe mode fails with a trailing slash under certain PHP versions. |
401 $path = untrailingslashit($path); |
498 $path = untrailingslashit( $path ); |
402 if ( empty($path) ) |
499 if ( empty( $path ) ) { |
403 return false; |
500 return false; |
404 |
501 } |
405 if ( ! $chmod ) |
502 |
|
503 if ( ! $chmod ) { |
406 $chmod = FS_CHMOD_DIR; |
504 $chmod = FS_CHMOD_DIR; |
407 |
505 } |
408 if ( ! @mkdir($path) ) |
506 |
409 return false; |
507 if ( ! @mkdir( $path ) ) { |
410 $this->chmod($path, $chmod); |
508 return false; |
411 if ( $chown ) |
509 } |
412 $this->chown($path, $chown); |
510 $this->chmod( $path, $chmod ); |
413 if ( $chgrp ) |
511 if ( $chown ) { |
414 $this->chgrp($path, $chgrp); |
512 $this->chown( $path, $chown ); |
|
513 } |
|
514 if ( $chgrp ) { |
|
515 $this->chgrp( $path, $chgrp ); |
|
516 } |
415 return true; |
517 return true; |
416 } |
518 } |
417 |
519 |
418 /** |
520 /** |
419 * |
521 * Deletes a directory. |
420 * @param string $path |
522 * |
421 * @param bool $recursive |
523 * @since 2.5.0 |
422 * @return bool |
524 * |
423 */ |
525 * @param string $path Path to directory. |
424 public function rmdir($path, $recursive = false) { |
526 * @param bool $recursive Optional. Whether to recursively remove files/directories. |
425 return $this->delete($path, $recursive); |
527 * Default false. |
426 } |
528 * @return bool True on success, false on failure. |
427 |
529 */ |
428 /** |
530 public function rmdir( $path, $recursive = false ) { |
429 * |
531 return $this->delete( $path, $recursive ); |
430 * @param string $path |
532 } |
431 * @param bool $include_hidden |
533 |
432 * @param bool $recursive |
534 /** |
433 * @return bool|array |
535 * Gets details for files in a directory or a specific file. |
434 */ |
536 * |
435 public function dirlist($path, $include_hidden = true, $recursive = false) { |
537 * @since 2.5.0 |
436 if ( $this->is_file($path) ) { |
538 * |
437 $limit_file = basename($path); |
539 * @param string $path Path to directory or file. |
438 $path = dirname($path); |
540 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
|
541 * Default true. |
|
542 * @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
|
543 * Default false. |
|
544 * @return array|false { |
|
545 * Array of files. False if unable to list directory contents. |
|
546 * |
|
547 * @type string $name Name of the file or directory. |
|
548 * @type string $perms *nix representation of permissions. |
|
549 * @type int $permsn Octal representation of permissions. |
|
550 * @type string $owner Owner name or ID. |
|
551 * @type int $size Size of file in bytes. |
|
552 * @type int $lastmodunix Last modified unix timestamp. |
|
553 * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
|
554 * @type int $time Last modified time. |
|
555 * @type string $type Type of resource. 'f' for file, 'd' for directory. |
|
556 * @type mixed $files If a directory and $recursive is true, contains another array of files. |
|
557 * } |
|
558 */ |
|
559 public function dirlist( $path, $include_hidden = true, $recursive = false ) { |
|
560 if ( $this->is_file( $path ) ) { |
|
561 $limit_file = basename( $path ); |
|
562 $path = dirname( $path ); |
439 } else { |
563 } else { |
440 $limit_file = false; |
564 $limit_file = false; |
441 } |
565 } |
442 |
566 |
443 if ( ! $this->is_dir($path) ) |
567 if ( ! $this->is_dir( $path ) ) { |
444 return false; |
568 return false; |
445 |
569 } |
446 $dir = @dir($path); |
570 |
447 if ( ! $dir ) |
571 $dir = @dir( $path ); |
448 return false; |
572 if ( ! $dir ) { |
|
573 return false; |
|
574 } |
449 |
575 |
450 $ret = array(); |
576 $ret = array(); |
451 |
577 |
452 while (false !== ($entry = $dir->read()) ) { |
578 while ( false !== ( $entry = $dir->read() ) ) { |
453 $struc = array(); |
579 $struc = array(); |
454 $struc['name'] = $entry; |
580 $struc['name'] = $entry; |
455 |
581 |
456 if ( '.' == $struc['name'] || '..' == $struc['name'] ) |
582 if ( '.' == $struc['name'] || '..' == $struc['name'] ) { |
457 continue; |
583 continue; |
458 |
584 } |
459 if ( ! $include_hidden && '.' == $struc['name'][0] ) |
585 |
|
586 if ( ! $include_hidden && '.' == $struc['name'][0] ) { |
460 continue; |
587 continue; |
461 |
588 } |
462 if ( $limit_file && $struc['name'] != $limit_file) |
589 |
|
590 if ( $limit_file && $struc['name'] != $limit_file ) { |
463 continue; |
591 continue; |
464 |
592 } |
465 $struc['perms'] = $this->gethchmod($path.'/'.$entry); |
593 |
466 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']); |
594 $struc['perms'] = $this->gethchmod( $path . '/' . $entry ); |
467 $struc['number'] = false; |
595 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); |
468 $struc['owner'] = $this->owner($path.'/'.$entry); |
596 $struc['number'] = false; |
469 $struc['group'] = $this->group($path.'/'.$entry); |
597 $struc['owner'] = $this->owner( $path . '/' . $entry ); |
470 $struc['size'] = $this->size($path.'/'.$entry); |
598 $struc['group'] = $this->group( $path . '/' . $entry ); |
471 $struc['lastmodunix']= $this->mtime($path.'/'.$entry); |
599 $struc['size'] = $this->size( $path . '/' . $entry ); |
472 $struc['lastmod'] = date('M j',$struc['lastmodunix']); |
600 $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); |
473 $struc['time'] = date('h:i:s',$struc['lastmodunix']); |
601 $struc['lastmod'] = date( 'M j', $struc['lastmodunix'] ); |
474 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; |
602 $struc['time'] = date( 'h:i:s', $struc['lastmodunix'] ); |
|
603 $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; |
475 |
604 |
476 if ( 'd' == $struc['type'] ) { |
605 if ( 'd' == $struc['type'] ) { |
477 if ( $recursive ) |
606 if ( $recursive ) { |
478 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive); |
607 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
479 else |
608 } else { |
480 $struc['files'] = array(); |
609 $struc['files'] = array(); |
|
610 } |
481 } |
611 } |
482 |
612 |
483 $ret[ $struc['name'] ] = $struc; |
613 $ret[ $struc['name'] ] = $struc; |
484 } |
614 } |
485 $dir->close(); |
615 $dir->close(); |
486 unset($dir); |
616 unset( $dir ); |
487 return $ret; |
617 return $ret; |
488 } |
618 } |
489 } |
619 } |