: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Moves a file or directory.
* After moving files or directories, OPcache will need to be invalidated.
* If moving a directory fails, `copy_dir()` can be used for a recursive copy.
* Use `move_dir()` for moving directories with OPcache invalidation and a
* fallback to `copy_dir()`.
* @param string $source Path to the source file or directory.
* @param string $destination Path to the destination file or directory.
* @param bool $overwrite Optional. Whether to overwrite the destination if it exists.
* @return bool True on success, false on failure.
public function move( $source, $destination, $overwrite = false ) {
if ( $this->exists( $destination ) ) {
// We need to remove the destination before we can rename the source.
$this->delete( $destination, false, 'f' );
// If we're not overwriting, the rename will fail, so return early.
return ssh2_sftp_rename( $this->sftp_link, $source, $destination );
* Deletes a file or directory.
* @param string $file Path to the file or directory.
* @param bool $recursive Optional. If set to true, deletes files and folders recursively.
* @param string|false $type Type of resource. 'f' for file, 'd' for directory.
* @return bool True on success, false on failure.
public function delete( $file, $recursive = false, $type = false ) {
if ( 'f' === $type || $this->is_file( $file ) ) {
return ssh2_sftp_unlink( $this->sftp_link, $file );
return ssh2_sftp_rmdir( $this->sftp_link, $file );
$filelist = $this->dirlist( $file );
if ( is_array( $filelist ) ) {
foreach ( $filelist as $filename => $fileinfo ) {
$this->delete( $file . '/' . $filename, $recursive, $fileinfo['type'] );
return ssh2_sftp_rmdir( $this->sftp_link, $file );
* Checks if a file or directory exists.
* @param string $path Path to file or directory.
* @return bool Whether $path exists or not.
public function exists( $path ) {
return file_exists( $this->sftp_path( $path ) );
* Checks if resource is a file.
* @param string $file File path.
* @return bool Whether $file is a file.
public function is_file( $file ) {
return is_file( $this->sftp_path( $file ) );
* Checks if resource is a directory.
* @param string $path Directory path.
* @return bool Whether $path is a directory.
public function is_dir( $path ) {
return is_dir( $this->sftp_path( $path ) );
* Checks if a file is readable.
* @param string $file Path to file.
* @return bool Whether $file is readable.
public function is_readable( $file ) {
return is_readable( $this->sftp_path( $file ) );
* Checks if a file or directory is writable.
* @param string $path Path to file or directory.
* @return bool Whether $path is writable.
public function is_writable( $path ) {
// PHP will base its writable checks on system_user === file_owner, not ssh_user === file_owner.
* Gets the file's last access time.
* @param string $file Path to file.
* @return int|false Unix timestamp representing last access time, false on failure.
public function atime( $file ) {
return fileatime( $this->sftp_path( $file ) );
* Gets the file modification time.
* @param string $file Path to file.
* @return int|false Unix timestamp representing modification time, false on failure.
public function mtime( $file ) {
return filemtime( $this->sftp_path( $file ) );
* Gets the file size (in bytes).
* @param string $file Path to file.
* @return int|false Size of the file in bytes on success, false on failure.
public function size( $file ) {
return filesize( $this->sftp_path( $file ) );
* Sets the access and modification times of a file.
* @param string $file Path to file.
* @param int $time Optional. Modified time to set for file.
* @param int $atime Optional. Access time to set for file.
public function touch( $file, $time = 0, $atime = 0 ) {
* @param string $path Path for new directory.
* @param int|false $chmod Optional. The permissions as octal number (or false to skip chmod).
* @param string|int|false $chown Optional. A user name or number (or false to skip chown).
* @param string|int|false $chgrp Optional. A group name or number (or false to skip chgrp).
* @return bool True on success, false on failure.
public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) {
$path = untrailingslashit( $path );
if ( ! ssh2_sftp_mkdir( $this->sftp_link, $path, $chmod, true ) ) {
// Set directory permissions.
ssh2_sftp_chmod( $this->sftp_link, $path, $chmod );
$this->chown( $path, $chown );
$this->chgrp( $path, $chgrp );
* @param string $path Path to directory.
* @param bool $recursive Optional. Whether to recursively remove files/directories.
* @return bool True on success, false on failure.
public function rmdir( $path, $recursive = false ) {
return $this->delete( $path, $recursive );
* Gets details for files in a directory or a specific file.
* @param string $path Path to directory or file.
* @param bool $include_hidden Optional. Whether to include details of hidden ("." prefixed) files.
* @param bool $recursive Optional. Whether to recursively include file details in nested directories.
* Array of arrays containing file information. False if unable to list directory contents.
* Array of file information. Note that some elements may not be available on all filesystems.
* @type string $name Name of the file or directory.
* @type string $perms *nix representation of permissions.
* @type string $permsn Octal representation of permissions.
* @type false $number File number. Always false in this context.
* @type string|false $owner Owner name or ID, or false if not available.
* @type string|false $group File permissions group, or false if not available.
* @type int|string|false $size Size of file in bytes. May be a numeric string.
* False if not available.
* @type int|string|false $lastmodunix Last modified unix timestamp. May be a numeric string.
* False if not available.
* @type string|false $lastmod Last modified month (3 letters) and day (without leading 0), or
* false if not available.
* @type string|false $time Last modified time, or false if not available.
* @type string $type Type of resource. 'f' for file, 'd' for directory, 'l' for link.
* @type array|false $files If a directory and `$recursive` is true, contains another array of
* files. False if unable to list directory contents.
public function dirlist( $path, $include_hidden = true, $recursive = false ) {
if ( $this->is_file( $path ) ) {
$limit_file = basename( $path );
$path = dirname( $path );
if ( ! $this->is_dir( $path ) || ! $this->is_readable( $path ) ) {
$dir = dir( $this->sftp_path( $path ) );
$path = trailingslashit( $path );
while ( false !== ( $entry = $dir->read() ) ) {
if ( '.' === $struc['name'] || '..' === $struc['name'] ) {
continue; // Do not care about these folders.
if ( ! $include_hidden && '.' === $struc['name'][0] ) {
if ( $limit_file && $struc['name'] !== $limit_file ) {
$struc['perms'] = $this->gethchmod( $path . $entry );
$struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] );
$struc['number'] = false;
$struc['owner'] = $this->owner( $path . $entry );
$struc['group'] = $this->group( $path . $entry );
$struc['size'] = $this->size( $path . $entry );
$struc['lastmodunix'] = $this->mtime( $path . $entry );
$struc['lastmod'] = gmdate( 'M j', $struc['lastmodunix'] );
$struc['time'] = gmdate( 'h:i:s', $struc['lastmodunix'] );
$struc['type'] = $this->is_dir( $path . $entry ) ? 'd' : 'f';
if ( 'd' === $struc['type'] ) {
$struc['files'] = $this->dirlist( $path . $struc['name'], $include_hidden, $recursive );
$struc['files'] = array();
$ret[ $struc['name'] ] = $struc;