31 public function __construct( $opt = '' ) { |
31 public function __construct( $opt = '' ) { |
32 $this->method = 'ftpsockets'; |
32 $this->method = 'ftpsockets'; |
33 $this->errors = new WP_Error(); |
33 $this->errors = new WP_Error(); |
34 |
34 |
35 // Check if possible to use ftp functions. |
35 // Check if possible to use ftp functions. |
36 if ( ! include_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) { |
36 if ( ! require_once ABSPATH . 'wp-admin/includes/class-ftp.php' ) { |
37 return; |
37 return; |
38 } |
38 } |
39 |
39 |
40 $this->ftp = new ftp(); |
40 $this->ftp = new ftp(); |
41 |
41 |
366 |
366 |
367 return $this->put_contents( $destination, $content, $mode ); |
367 return $this->put_contents( $destination, $content, $mode ); |
368 } |
368 } |
369 |
369 |
370 /** |
370 /** |
371 * Moves a file. |
371 * Moves a file or directory. |
372 * |
372 * |
373 * @since 2.5.0 |
373 * After moving files or directories, OPcache will need to be invalidated. |
374 * |
374 * |
375 * @param string $source Path to the source file. |
375 * If moving a directory fails, `copy_dir()` can be used for a recursive copy. |
376 * @param string $destination Path to the destination file. |
376 * |
377 * @param bool $overwrite Optional. Whether to overwrite the destination file if it exists. |
377 * Use `move_dir()` for moving directories with OPcache invalidation and a |
|
378 * fallback to `copy_dir()`. |
|
379 * |
|
380 * @since 2.5.0 |
|
381 * |
|
382 * @param string $source Path to the source file or directory. |
|
383 * @param string $destination Path to the destination file or directory. |
|
384 * @param bool $overwrite Optional. Whether to overwrite the destination if it exists. |
378 * Default false. |
385 * Default false. |
379 * @return bool True on success, false on failure. |
386 * @return bool True on success, false on failure. |
380 */ |
387 */ |
381 public function move( $source, $destination, $overwrite = false ) { |
388 public function move( $source, $destination, $overwrite = false ) { |
382 return $this->ftp->rename( $source, $destination ); |
389 return $this->ftp->rename( $source, $destination ); |
412 |
419 |
413 /** |
420 /** |
414 * Checks if a file or directory exists. |
421 * Checks if a file or directory exists. |
415 * |
422 * |
416 * @since 2.5.0 |
423 * @since 2.5.0 |
417 * |
424 * @since 6.3.0 Returns false for an empty path. |
418 * @param string $file Path to file or directory. |
425 * |
419 * @return bool Whether $file exists or not. |
426 * @param string $path Path to file or directory. |
420 */ |
427 * @return bool Whether $path exists or not. |
421 public function exists( $file ) { |
428 */ |
422 $list = $this->ftp->nlist( $file ); |
429 public function exists( $path ) { |
423 |
430 /* |
424 if ( empty( $list ) && $this->is_dir( $file ) ) { |
431 * Check for empty path. If ftp::nlist() receives an empty path, |
|
432 * it checks the current working directory and may return true. |
|
433 * |
|
434 * See https://core.trac.wordpress.org/ticket/33058. |
|
435 */ |
|
436 if ( '' === $path ) { |
|
437 return false; |
|
438 } |
|
439 |
|
440 $list = $this->ftp->nlist( $path ); |
|
441 |
|
442 if ( empty( $list ) && $this->is_dir( $path ) ) { |
425 return true; // File is an empty directory. |
443 return true; // File is an empty directory. |
426 } |
444 } |
427 |
445 |
428 return ! empty( $list ); // Empty list = no file, so invert. |
446 return ! empty( $list ); // Empty list = no file, so invert. |
429 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server. |
447 // Return $this->ftp->is_exists($file); has issues with ABOR+426 responses on the ncFTPd server. |
603 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
621 * @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files. |
604 * Default true. |
622 * Default true. |
605 * @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
623 * @param bool $recursive Optional. Whether to recursively include file details in nested directories. |
606 * Default false. |
624 * Default false. |
607 * @return array|false { |
625 * @return array|false { |
608 * Array of files. False if unable to list directory contents. |
626 * Array of arrays containing file information. False if unable to list directory contents. |
609 * |
627 * |
610 * @type string $name Name of the file or directory. |
628 * @type array ...$0 { |
611 * @type string $perms *nix representation of permissions. |
629 * Array of file information. Note that some elements may not be available on all filesystems. |
612 * @type string $permsn Octal representation of permissions. |
630 * |
613 * @type string $owner Owner name or ID. |
631 * @type string $name Name of the file or directory. |
614 * @type int $size Size of file in bytes. |
632 * @type string $perms *nix representation of permissions. |
615 * @type int $lastmodunix Last modified unix timestamp. |
633 * @type string $permsn Octal representation of permissions. |
616 * @type mixed $lastmod Last modified month (3 letter) and day (without leading 0). |
634 * @type int|string|false $number File number. May be a numeric string. False if not available. |
617 * @type int $time Last modified time. |
635 * @type string|false $owner Owner name or ID, or false if not available. |
618 * @type string $type Type of resource. 'f' for file, 'd' for directory. |
636 * @type string|false $group File permissions group, or false if not available. |
619 * @type mixed $files If a directory and `$recursive` is true, contains another array of files. |
637 * @type int|string|false $size Size of file in bytes. May be a numeric string. |
|
638 * False if not available. |
|
639 * @type int|string|false $lastmodunix Last modified unix timestamp. May be a numeric string. |
|
640 * False if not available. |
|
641 * @type string|false $lastmod Last modified month (3 letters) and day (without leading 0), or |
|
642 * false if not available. |
|
643 * @type string|false $time Last modified time, or false if not available. |
|
644 * @type string $type Type of resource. 'f' for file, 'd' for directory, 'l' for link. |
|
645 * @type array|false $files If a directory and `$recursive` is true, contains another array of |
|
646 * files. False if unable to list directory contents. |
|
647 * } |
620 * } |
648 * } |
621 */ |
649 */ |
622 public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { |
650 public function dirlist( $path = '.', $include_hidden = true, $recursive = false ) { |
623 if ( $this->is_file( $path ) ) { |
651 if ( $this->is_file( $path ) ) { |
624 $limit_file = basename( $path ); |
652 $limit_file = basename( $path ); |