62 * Default false. |
62 * Default false. |
63 * @return bool True on success, false on failure. |
63 * @return bool True on success, false on failure. |
64 */ |
64 */ |
65 public function put_contents( $file, $contents, $mode = false ) { |
65 public function put_contents( $file, $contents, $mode = false ) { |
66 $fp = @fopen( $file, 'wb' ); |
66 $fp = @fopen( $file, 'wb' ); |
|
67 |
67 if ( ! $fp ) { |
68 if ( ! $fp ) { |
68 return false; |
69 return false; |
69 } |
70 } |
70 |
71 |
71 mbstring_binary_safe_encoding(); |
72 mbstring_binary_safe_encoding(); |
123 */ |
124 */ |
124 public function chgrp( $file, $group, $recursive = false ) { |
125 public function chgrp( $file, $group, $recursive = false ) { |
125 if ( ! $this->exists( $file ) ) { |
126 if ( ! $this->exists( $file ) ) { |
126 return false; |
127 return false; |
127 } |
128 } |
|
129 |
128 if ( ! $recursive ) { |
130 if ( ! $recursive ) { |
129 return @chgrp( $file, $group ); |
131 return chgrp( $file, $group ); |
130 } |
132 } |
|
133 |
131 if ( ! $this->is_dir( $file ) ) { |
134 if ( ! $this->is_dir( $file ) ) { |
132 return @chgrp( $file, $group ); |
135 return chgrp( $file, $group ); |
133 } |
136 } |
134 // Is a directory, and we want recursive |
137 |
|
138 // Is a directory, and we want recursive. |
135 $file = trailingslashit( $file ); |
139 $file = trailingslashit( $file ); |
136 $filelist = $this->dirlist( $file ); |
140 $filelist = $this->dirlist( $file ); |
|
141 |
137 foreach ( $filelist as $filename ) { |
142 foreach ( $filelist as $filename ) { |
138 $this->chgrp( $file . $filename, $group, $recursive ); |
143 $this->chgrp( $file . $filename, $group, $recursive ); |
139 } |
144 } |
140 |
145 |
141 return true; |
146 return true; |
147 * @since 2.5.0 |
152 * @since 2.5.0 |
148 * |
153 * |
149 * @param string $file Path to the file. |
154 * @param string $file Path to the file. |
150 * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
155 * @param int|false $mode Optional. The permissions as octal number, usually 0644 for files, |
151 * 0755 for directories. Default false. |
156 * 0755 for directories. Default false. |
152 * @param bool $recursive Optional. If set to true, changes file group recursively. |
157 * @param bool $recursive Optional. If set to true, changes file permissions recursively. |
153 * Default false. |
158 * Default false. |
154 * @return bool True on success, false on failure. |
159 * @return bool True on success, false on failure. |
155 */ |
160 */ |
156 public function chmod( $file, $mode = false, $recursive = false ) { |
161 public function chmod( $file, $mode = false, $recursive = false ) { |
157 if ( ! $mode ) { |
162 if ( ! $mode ) { |
163 return false; |
168 return false; |
164 } |
169 } |
165 } |
170 } |
166 |
171 |
167 if ( ! $recursive || ! $this->is_dir( $file ) ) { |
172 if ( ! $recursive || ! $this->is_dir( $file ) ) { |
168 return @chmod( $file, $mode ); |
173 return chmod( $file, $mode ); |
169 } |
174 } |
170 // Is a directory, and we want recursive |
175 |
|
176 // Is a directory, and we want recursive. |
171 $file = trailingslashit( $file ); |
177 $file = trailingslashit( $file ); |
172 $filelist = $this->dirlist( $file ); |
178 $filelist = $this->dirlist( $file ); |
|
179 |
173 foreach ( (array) $filelist as $filename => $filemeta ) { |
180 foreach ( (array) $filelist as $filename => $filemeta ) { |
174 $this->chmod( $file . $filename, $mode, $recursive ); |
181 $this->chmod( $file . $filename, $mode, $recursive ); |
175 } |
182 } |
176 |
183 |
177 return true; |
184 return true; |
190 */ |
197 */ |
191 public function chown( $file, $owner, $recursive = false ) { |
198 public function chown( $file, $owner, $recursive = false ) { |
192 if ( ! $this->exists( $file ) ) { |
199 if ( ! $this->exists( $file ) ) { |
193 return false; |
200 return false; |
194 } |
201 } |
|
202 |
195 if ( ! $recursive ) { |
203 if ( ! $recursive ) { |
196 return @chown( $file, $owner ); |
204 return chown( $file, $owner ); |
197 } |
205 } |
|
206 |
198 if ( ! $this->is_dir( $file ) ) { |
207 if ( ! $this->is_dir( $file ) ) { |
199 return @chown( $file, $owner ); |
208 return chown( $file, $owner ); |
200 } |
209 } |
201 // Is a directory, and we want recursive |
210 |
|
211 // Is a directory, and we want recursive. |
202 $filelist = $this->dirlist( $file ); |
212 $filelist = $this->dirlist( $file ); |
|
213 |
203 foreach ( $filelist as $filename ) { |
214 foreach ( $filelist as $filename ) { |
204 $this->chown( $file . '/' . $filename, $owner, $recursive ); |
215 $this->chown( $file . '/' . $filename, $owner, $recursive ); |
205 } |
216 } |
|
217 |
206 return true; |
218 return true; |
207 } |
219 } |
208 |
220 |
209 /** |
221 /** |
210 * Gets the file owner. |
222 * Gets the file owner. |
214 * @param string $file Path to the file. |
226 * @param string $file Path to the file. |
215 * @return string|false Username of the owner on success, false on failure. |
227 * @return string|false Username of the owner on success, false on failure. |
216 */ |
228 */ |
217 public function owner( $file ) { |
229 public function owner( $file ) { |
218 $owneruid = @fileowner( $file ); |
230 $owneruid = @fileowner( $file ); |
|
231 |
219 if ( ! $owneruid ) { |
232 if ( ! $owneruid ) { |
220 return false; |
233 return false; |
221 } |
234 } |
|
235 |
222 if ( ! function_exists( 'posix_getpwuid' ) ) { |
236 if ( ! function_exists( 'posix_getpwuid' ) ) { |
223 return $owneruid; |
237 return $owneruid; |
224 } |
238 } |
|
239 |
225 $ownerarray = posix_getpwuid( $owneruid ); |
240 $ownerarray = posix_getpwuid( $owneruid ); |
|
241 |
|
242 if ( ! $ownerarray ) { |
|
243 return false; |
|
244 } |
|
245 |
226 return $ownerarray['name']; |
246 return $ownerarray['name']; |
227 } |
247 } |
228 |
248 |
229 /** |
249 /** |
230 * Gets the permissions of the specified file or filepath in their octal format. |
250 * Gets the permissions of the specified file or filepath in their octal format. |
248 * @param string $file Path to the file. |
268 * @param string $file Path to the file. |
249 * @return string|false The group on success, false on failure. |
269 * @return string|false The group on success, false on failure. |
250 */ |
270 */ |
251 public function group( $file ) { |
271 public function group( $file ) { |
252 $gid = @filegroup( $file ); |
272 $gid = @filegroup( $file ); |
|
273 |
253 if ( ! $gid ) { |
274 if ( ! $gid ) { |
254 return false; |
275 return false; |
255 } |
276 } |
|
277 |
256 if ( ! function_exists( 'posix_getgrgid' ) ) { |
278 if ( ! function_exists( 'posix_getgrgid' ) ) { |
257 return $gid; |
279 return $gid; |
258 } |
280 } |
|
281 |
259 $grouparray = posix_getgrgid( $gid ); |
282 $grouparray = posix_getgrgid( $gid ); |
|
283 |
|
284 if ( ! $grouparray ) { |
|
285 return false; |
|
286 } |
|
287 |
260 return $grouparray['name']; |
288 return $grouparray['name']; |
261 } |
289 } |
262 |
290 |
263 /** |
291 /** |
264 * Copies a file. |
292 * Copies a file. |
318 * Deletes a file or directory. |
349 * Deletes a file or directory. |
319 * |
350 * |
320 * @since 2.5.0 |
351 * @since 2.5.0 |
321 * |
352 * |
322 * @param string $file Path to the file or directory. |
353 * @param string $file Path to the file or directory. |
323 * @param bool $recursive Optional. If set to true, changes file group recursively. |
354 * @param bool $recursive Optional. If set to true, deletes files and folders recursively. |
324 * Default false. |
355 * Default false. |
325 * @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
356 * @param string|false $type Type of resource. 'f' for file, 'd' for directory. |
326 * Default false. |
357 * Default false. |
327 * @return bool True on success, false on failure. |
358 * @return bool True on success, false on failure. |
328 */ |
359 */ |
329 public function delete( $file, $recursive = false, $type = false ) { |
360 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. |
361 if ( empty( $file ) ) { |
331 return false; |
362 // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. |
332 } |
363 return false; |
333 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise |
364 } |
334 |
365 |
335 if ( 'f' == $type || $this->is_file( $file ) ) { |
366 $file = str_replace( '\\', '/', $file ); // For Win32, occasional problems deleting files otherwise. |
|
367 |
|
368 if ( 'f' === $type || $this->is_file( $file ) ) { |
336 return @unlink( $file ); |
369 return @unlink( $file ); |
337 } |
370 } |
|
371 |
338 if ( ! $recursive && $this->is_dir( $file ) ) { |
372 if ( ! $recursive && $this->is_dir( $file ) ) { |
339 return @rmdir( $file ); |
373 return @rmdir( $file ); |
340 } |
374 } |
341 |
375 |
342 // At this point it's a folder, and we're in recursive mode |
376 // At this point it's a folder, and we're in recursive mode. |
343 $file = trailingslashit( $file ); |
377 $file = trailingslashit( $file ); |
344 $filelist = $this->dirlist( $file, true ); |
378 $filelist = $this->dirlist( $file, true ); |
345 |
379 |
346 $retval = true; |
380 $retval = true; |
|
381 |
347 if ( is_array( $filelist ) ) { |
382 if ( is_array( $filelist ) ) { |
348 foreach ( $filelist as $filename => $fileinfo ) { |
383 foreach ( $filelist as $filename => $fileinfo ) { |
349 if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { |
384 if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { |
350 $retval = false; |
385 $retval = false; |
351 } |
386 } |
468 * @param int $atime Optional. Access time to set for file. |
503 * @param int $atime Optional. Access time to set for file. |
469 * Default 0. |
504 * Default 0. |
470 * @return bool True on success, false on failure. |
505 * @return bool True on success, false on failure. |
471 */ |
506 */ |
472 public function touch( $file, $time = 0, $atime = 0 ) { |
507 public function touch( $file, $time = 0, $atime = 0 ) { |
473 if ( $time == 0 ) { |
508 if ( 0 == $time ) { |
474 $time = time(); |
509 $time = time(); |
475 } |
510 } |
476 if ( $atime == 0 ) { |
511 |
|
512 if ( 0 == $atime ) { |
477 $atime = time(); |
513 $atime = time(); |
478 } |
514 } |
479 return @touch( $file, $time, $atime ); |
515 |
|
516 return touch( $file, $time, $atime ); |
480 } |
517 } |
481 |
518 |
482 /** |
519 /** |
483 * Creates a directory. |
520 * Creates a directory. |
484 * |
521 * |
494 * @return bool True on success, false on failure. |
531 * @return bool True on success, false on failure. |
495 */ |
532 */ |
496 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
533 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { |
497 // Safe mode fails with a trailing slash under certain PHP versions. |
534 // Safe mode fails with a trailing slash under certain PHP versions. |
498 $path = untrailingslashit( $path ); |
535 $path = untrailingslashit( $path ); |
|
536 |
499 if ( empty( $path ) ) { |
537 if ( empty( $path ) ) { |
500 return false; |
538 return false; |
501 } |
539 } |
502 |
540 |
503 if ( ! $chmod ) { |
541 if ( ! $chmod ) { |
505 } |
543 } |
506 |
544 |
507 if ( ! @mkdir( $path ) ) { |
545 if ( ! @mkdir( $path ) ) { |
508 return false; |
546 return false; |
509 } |
547 } |
|
548 |
510 $this->chmod( $path, $chmod ); |
549 $this->chmod( $path, $chmod ); |
|
550 |
511 if ( $chown ) { |
551 if ( $chown ) { |
512 $this->chown( $path, $chown ); |
552 $this->chown( $path, $chown ); |
513 } |
553 } |
|
554 |
514 if ( $chgrp ) { |
555 if ( $chgrp ) { |
515 $this->chgrp( $path, $chgrp ); |
556 $this->chgrp( $path, $chgrp ); |
516 } |
557 } |
|
558 |
517 return true; |
559 return true; |
518 } |
560 } |
519 |
561 |
520 /** |
562 /** |
521 * Deletes a directory. |
563 * Deletes a directory. |
562 $path = dirname( $path ); |
604 $path = dirname( $path ); |
563 } else { |
605 } else { |
564 $limit_file = false; |
606 $limit_file = false; |
565 } |
607 } |
566 |
608 |
567 if ( ! $this->is_dir( $path ) ) { |
609 if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) { |
568 return false; |
610 return false; |
569 } |
611 } |
570 |
612 |
571 $dir = @dir( $path ); |
613 $dir = dir( $path ); |
|
614 |
572 if ( ! $dir ) { |
615 if ( ! $dir ) { |
573 return false; |
616 return false; |
574 } |
617 } |
575 |
618 |
576 $ret = array(); |
619 $ret = array(); |
577 |
620 |
578 while ( false !== ( $entry = $dir->read() ) ) { |
621 while ( false !== ( $entry = $dir->read() ) ) { |
579 $struc = array(); |
622 $struc = array(); |
580 $struc['name'] = $entry; |
623 $struc['name'] = $entry; |
581 |
624 |
582 if ( '.' == $struc['name'] || '..' == $struc['name'] ) { |
625 if ( '.' === $struc['name'] || '..' === $struc['name'] ) { |
583 continue; |
626 continue; |
584 } |
627 } |
585 |
628 |
586 if ( ! $include_hidden && '.' == $struc['name'][0] ) { |
629 if ( ! $include_hidden && '.' === $struc['name'][0] ) { |
587 continue; |
630 continue; |
588 } |
631 } |
589 |
632 |
590 if ( $limit_file && $struc['name'] != $limit_file ) { |
633 if ( $limit_file && $struc['name'] != $limit_file ) { |
591 continue; |
634 continue; |
596 $struc['number'] = false; |
639 $struc['number'] = false; |
597 $struc['owner'] = $this->owner( $path . '/' . $entry ); |
640 $struc['owner'] = $this->owner( $path . '/' . $entry ); |
598 $struc['group'] = $this->group( $path . '/' . $entry ); |
641 $struc['group'] = $this->group( $path . '/' . $entry ); |
599 $struc['size'] = $this->size( $path . '/' . $entry ); |
642 $struc['size'] = $this->size( $path . '/' . $entry ); |
600 $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); |
643 $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); |
601 $struc['lastmod'] = date( 'M j', $struc['lastmodunix'] ); |
644 $struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] ); |
602 $struc['time'] = date( 'h:i:s', $struc['lastmodunix'] ); |
645 $struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] ); |
603 $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; |
646 $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; |
604 |
647 |
605 if ( 'd' == $struc['type'] ) { |
648 if ( 'd' === $struc['type'] ) { |
606 if ( $recursive ) { |
649 if ( $recursive ) { |
607 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
650 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); |
608 } else { |
651 } else { |
609 $struc['files'] = array(); |
652 $struc['files'] = array(); |
610 } |
653 } |
611 } |
654 } |
612 |
655 |
613 $ret[ $struc['name'] ] = $struc; |
656 $ret[ $struc['name'] ] = $struc; |
614 } |
657 } |
|
658 |
615 $dir->close(); |
659 $dir->close(); |
616 unset( $dir ); |
660 unset( $dir ); |
|
661 |
617 return $ret; |
662 return $ret; |
618 } |
663 } |
619 } |
664 } |