* Base WordPress Filesystem
* Base WordPress Filesystem class which Filesystem implementations extend.
#[AllowDynamicProperties]
class WP_Filesystem_Base {
* Whether to display debug data for the connection.
* Cached list of local filepaths to mapped remote filepaths.
* The Access method of the current connection, Set automatically.
public $options = array();
* Returns the path on the remote filesystem of ABSPATH.
* @return string The location of the remote path.
public function abspath() {
$folder = $this->find_folder( ABSPATH );
* Perhaps the FTP folder is rooted at the WordPress install.
* Check for wp-includes folder in root. Could have some false positives, but rare.
if ( ! $folder && $this->is_dir( '/' . WPINC ) ) {
* Returns the path on the remote filesystem of WP_CONTENT_DIR.
* @return string The location of the remote path.
public function wp_content_dir() {
return $this->find_folder( WP_CONTENT_DIR );
* Returns the path on the remote filesystem of WP_PLUGIN_DIR.
* @return string The location of the remote path.
public function wp_plugins_dir() {
return $this->find_folder( WP_PLUGIN_DIR );
* Returns the path on the remote filesystem of the Themes Directory.
* @param string|false $theme Optional. The theme stylesheet or template for the directory.
* @return string The location of the remote path.
public function wp_themes_dir( $theme = false ) {
$theme_root = get_theme_root( $theme );
// Account for relative theme roots.
if ( '/themes' === $theme_root || ! is_dir( $theme_root ) ) {
$theme_root = WP_CONTENT_DIR . $theme_root;
return $this->find_folder( $theme_root );
* Returns the path on the remote filesystem of WP_LANG_DIR.
* @return string The location of the remote path.
public function wp_lang_dir() {
return $this->find_folder( WP_LANG_DIR );
* Locates a folder on the remote filesystem.
* @deprecated 2.7.0 use WP_Filesystem_Base::abspath() or WP_Filesystem_Base::wp_*_dir() instead.
* @see WP_Filesystem_Base::abspath()
* @see WP_Filesystem_Base::wp_content_dir()
* @see WP_Filesystem_Base::wp_plugins_dir()
* @see WP_Filesystem_Base::wp_themes_dir()
* @see WP_Filesystem_Base::wp_lang_dir()
* @param string $base Optional. The folder to start searching from. Default '.'.
* @param bool $verbose Optional. True to display debug information. Default false.
* @return string The location of the remote path.
public function find_base_dir( $base = '.', $verbose = false ) {
_deprecated_function( __FUNCTION__, '2.7.0', 'WP_Filesystem_Base::abspath() or WP_Filesystem_Base::wp_*_dir()' );
$this->verbose = $verbose;
* Locates a folder on the remote filesystem.
* @deprecated 2.7.0 use WP_Filesystem_Base::abspath() or WP_Filesystem_Base::wp_*_dir() methods instead.
* @see WP_Filesystem_Base::abspath()
* @see WP_Filesystem_Base::wp_content_dir()
* @see WP_Filesystem_Base::wp_plugins_dir()
* @see WP_Filesystem_Base::wp_themes_dir()
* @see WP_Filesystem_Base::wp_lang_dir()
* @param string $base Optional. The folder to start searching from. Default '.'.
* @param bool $verbose Optional. True to display debug information. Default false.
* @return string The location of the remote path.
public function get_base_dir( $base = '.', $verbose = false ) {
_deprecated_function( __FUNCTION__, '2.7.0', 'WP_Filesystem_Base::abspath() or WP_Filesystem_Base::wp_*_dir()' );
$this->verbose = $verbose;
* Locates a folder on the remote filesystem.
* Assumes that on Windows systems, Stripping off the Drive
* letter is OK Sanitizes \\ to / in Windows filepaths.
* @param string $folder the folder to locate.
* @return string|false The location of the remote path, false on failure.
public function find_folder( $folder ) {
if ( isset( $this->cache[ $folder ] ) ) {
return $this->cache[ $folder ];
if ( stripos( $this->method, 'ftp' ) !== false ) {
$constant_overrides = array(
'FTP_CONTENT_DIR' => WP_CONTENT_DIR,
'FTP_PLUGIN_DIR' => WP_PLUGIN_DIR,
'FTP_LANG_DIR' => WP_LANG_DIR,
// Direct matches ( folder = CONSTANT/ ).
foreach ( $constant_overrides as $constant => $dir ) {
if ( ! defined( $constant ) ) {
if ( $folder === $dir ) {
return trailingslashit( constant( $constant ) );
// Prefix matches ( folder = CONSTANT/subdir ),
foreach ( $constant_overrides as $constant => $dir ) {
if ( ! defined( $constant ) ) {
if ( 0 === stripos( $folder, $dir ) ) { // $folder starts with $dir.
$potential_folder = preg_replace( '#^' . preg_quote( $dir, '#' ) . '/#i', trailingslashit( constant( $constant ) ), $folder );
$potential_folder = trailingslashit( $potential_folder );
if ( $this->is_dir( $potential_folder ) ) {
$this->cache[ $folder ] = $potential_folder;
return $potential_folder;
} elseif ( 'direct' === $this->method ) {
$folder = str_replace( '\\', '/', $folder ); // Windows path sanitization.
return trailingslashit( $folder );
$folder = preg_replace( '|^([a-z]{1}):|i', '', $folder ); // Strip out Windows drive letter if it's there.
$folder = str_replace( '\\', '/', $folder ); // Windows path sanitization.
if ( isset( $this->cache[ $folder ] ) ) {
return $this->cache[ $folder ];
if ( $this->exists( $folder ) ) { // Folder exists at that absolute path.
$folder = trailingslashit( $folder );
$this->cache[ $folder ] = $folder;
$return = $this->search_for_folder( $folder );
$this->cache[ $folder ] = $return;
* Locates a folder on the remote filesystem.
* Expects Windows sanitized path.
* @param string $folder The folder to locate.
* @param string $base The folder to start searching from.
* @param bool $loop If the function has recursed. Internal use only.
* @return string|false The location of the remote path, false to cease looping.
public function search_for_folder( $folder, $base = '.', $loop = false ) {
if ( empty( $base ) || '.' === $base ) {
$base = trailingslashit( $this->cwd() );
$folder = untrailingslashit( $folder );
/* translators: 1: Folder to locate, 2: Folder to start searching from. */
printf( "\n" . __( 'Looking for %1$s in %2$s' ) . "<br />\n", $folder, $base );
$folder_parts = explode( '/', $folder );
$folder_part_keys = array_keys( $folder_parts );
$last_index = array_pop( $folder_part_keys );
$last_path = $folder_parts[ $last_index ];
$files = $this->dirlist( $base );
foreach ( $folder_parts as $index => $key ) {
if ( $index === $last_index ) {
continue; // We want this to be caught by the next code block.
* Working from /home/ to /user/ to /wordpress/ see if that file exists within
* the current folder, If it's found, change into it and follow through looking
* for it. If it can't find WordPress down that route, it'll continue onto the next
* folder level, and see if that matches, and so on. If it reaches the end, and still
* can't find it, it'll return false for the entire function.
if ( isset( $files[ $key ] ) ) {
// Let's try that folder:
$newdir = trailingslashit( path_join( $base, $key ) );
/* translators: %s: Directory name. */
printf( "\n" . __( 'Changing to %s' ) . "<br />\n", $newdir );
// Only search for the remaining path tokens in the directory, not the full path again.
$newfolder = implode( '/', array_slice( $folder_parts, $index + 1 ) );
$ret = $this->search_for_folder( $newfolder, $newdir, $loop );
* Only check this as a last resort, to prevent locating the incorrect install.
* All above procedures will fail quickly if this is the right branch to take.
if ( isset( $files[ $last_path ] ) ) {
/* translators: %s: Directory name. */
printf( "\n" . __( 'Found %s' ) . "<br />\n", $base . $last_path );
return trailingslashit( $base . $last_path );
* Prevent this function from looping again.
* No need to proceed if we've just searched in `/`.
if ( $loop || '/' === $base ) {
* As an extra last resort, Change back to / if the folder wasn't found.
* This comes into effect when the CWD is /home/user/ but WP is at /var/www/....
return $this->search_for_folder( $folder, '/', true );
* Returns the *nix-style file permissions for a file.
* From the PHP documentation page for fileperms().
* @link https://www.php.net/manual/en/function.fileperms.php
* @param string $file String filename.
* @return string The *nix-style representation of permissions.
public function gethchmod( $file ) {
$perms = intval( $this->getchmod( $file ), 8 );
if ( ( $perms & 0xC000 ) === 0xC000 ) { // Socket.
} elseif ( ( $perms & 0xA000 ) === 0xA000 ) { // Symbolic Link.
} elseif ( ( $perms & 0x8000 ) === 0x8000 ) { // Regular.
} elseif ( ( $perms & 0x6000 ) === 0x6000 ) { // Block special.
} elseif ( ( $perms & 0x4000 ) === 0x4000 ) { // Directory.
} elseif ( ( $perms & 0x2000 ) === 0x2000 ) { // Character special.
} elseif ( ( $perms & 0x1000 ) === 0x1000 ) { // FIFO pipe.
$info .= ( ( $perms & 0x0100 ) ? 'r' : '-' );
$info .= ( ( $perms & 0x0080 ) ? 'w' : '-' );
$info .= ( ( $perms & 0x0040 ) ?
( ( $perms & 0x0800 ) ? 's' : 'x' ) :
( ( $perms & 0x0800 ) ? 'S' : '-' ) );
$info .= ( ( $perms & 0x0020 ) ? 'r' : '-' );
$info .= ( ( $perms & 0x0010 ) ? 'w' : '-' );
$info .= ( ( $perms & 0x0008 ) ?
( ( $perms & 0x0400 ) ? 's' : 'x' ) :
( ( $perms & 0x0400 ) ? 'S' : '-' ) );
$info .= ( ( $perms & 0x0004 ) ? 'r' : '-' );
$info .= ( ( $perms & 0x0002 ) ? 'w' : '-' );
$info .= ( ( $perms & 0x0001 ) ?
( ( $perms & 0x0200 ) ? 't' : 'x' ) :
( ( $perms & 0x0200 ) ? 'T' : '-' ) );
* Gets the permissions of the specified file or filepath in their octal format.
* @param string $file Path to the file.
* @return string Mode of the file (the last 3 digits).
public function getchmod( $file ) {
* Converts *nix-style file permissions to an octal number.
* Converts '-rw-r--r--' to 0644
* From "info at rvgate dot nl"'s comment on the PHP documentation for chmod()
* @link https://www.php.net/manual/en/function.chmod.php#49614
* @param string $mode string The *nix-style file permissions.
* @return string Octal representation of permissions.
public function getnumchmodfromh( $mode ) {
$legal = array( '', 'w', 'r', 'x', '-' );
$attarray = preg_split( '//', $mode );
for ( $i = 0, $c = count( $attarray ); $i < $c; $i++ ) {
$key = array_search( $attarray[ $i ], $legal, true );
$realmode .= $legal[ $key ];
$mode = str_pad( $realmode, 10, '-', STR_PAD_LEFT );
$mode = strtr( $mode, $trans );
$newmode .= $mode[1] + $mode[2] + $mode[3];
$newmode .= $mode[4] + $mode[5] + $mode[6];
$newmode .= $mode[7] + $mode[8] + $mode[9];
* Determines if the string provided contains binary characters.
* @param string $text String to test against.
* @return bool True if string is binary, false otherwise.
public function is_binary( $text ) {
return (bool) preg_match( '|[^\x20-\x7E]|', $text ); // chr(32)..chr(127)
* Changes the owner of a file or directory.
* Default behavior is to do nothing, override this in your subclass, if desired.
* @param string $file Path to the file or directory.
* @param string|int $owner A user name or number.
* @param bool $recursive Optional. If set to true, changes file owner recursively.
* @return bool True on success, false on failure.
public function chown( $file, $owner, $recursive = false ) {
* @return bool True on success, false on failure (always true for WP_Filesystem_Direct).
public function connect() {
* Reads entire file into a string.
* @param string $file Name of the file to read.
* @return string|false Read data on success, false on failure.
public function get_contents( $file ) {