get_filesystem_method( array $args = array(), string $context = '', bool $allow_relaxed_file_ownership = false ): string
- Since
- 2.5.0
- Source
wp-admin/includes/file.php:2271
Description
The priority of the transports are: Direct, SSH2, FTP PHP Extension, FTP Sockets (Via Sockets class, or fsockopen()). Valid values for these are: 'direct', 'ssh2', 'ftpext' or 'ftpsockets'.
The return value can be overridden by defining the FS_METHOD constant in wp-config.php, or filtering via 'filesystem_method'.
Compatibility
- WordPress
- since 2.5.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$argsarrayoptional- Connection details. Default empty array.Default:
array() $contextstringoptional- Full path to the directory that is tested for being writable. Default empty.Default:
'' $allow_relaxed_file_ownershipbooloptional- Whether to allow Group/World writable.
Default false.Default:false
Return value
string- The transport to use, see description for valid return values.
Performance profile
How much work a call to get_filesystem_method() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 24–97
- Plugin surface
- 1 hook
- Called by
- 5
Reads or writes the filesystem via is_dir().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 102.
Third-party callbacks on 'filesystem_method' run inside this call, and their cost is not bounded by anything here.
5 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
is_dir()called directly
What one call costs · 18 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_filesystem_method() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$context !== false | 24–27 | trailingslashit(), apply_filters() |
$context === false | 28–33 | is_dir(), trailingslashit(), apply_filters() |
$context !== false | 41–50 | trailingslashit(), uniqid(), fopen(), apply_filters() |
$context === false | 45–56 | is_dir(), trailingslashit(), uniqid(), fopen(), apply_filters() |
$context !== false | 49–56 | trailingslashit(), uniqid(), fopen(), extension_loaded(), apply_filters() |
$context !== false | 53–61 | trailingslashit(), uniqid(), fopen(), extension_loaded(), extension_loaded(), apply_filters() |
$context === false | 53–62 | is_dir(), trailingslashit(), uniqid(), fopen(), extension_loaded(), apply_filters() |
$context === false | 57–67 | is_dir(), trailingslashit(), uniqid(), fopen(), extension_loaded(), extension_loaded(), apply_filters() |
$context !== false && isset($args) | 60–65 | trailingslashit(), uniqid(), fopen(), extension_loaded(), extension_loaded(), extension_loaded(), apply_filters() |
$context !== false | 62–76 | trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), apply_filters() |
$context === false && isset($args) | 64–71 | is_dir(), trailingslashit(), uniqid(), fopen(), extension_loaded(), extension_loaded(), extension_loaded(), apply_filters() |
$context === false | 66–82 | is_dir(), trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), apply_filters() |
6 further outcomes, up to 97 instructions
$context !== false | 70–82 | trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), apply_filters() |
$context !== false | 74–87 | trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), extension_loaded(), apply_filters() |
$context === false | 74–88 | is_dir(), trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), apply_filters() |
$context === false | 78–93 | is_dir(), trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), extension_loaded(), apply_filters() |
$context !== false && isset($args) | 81–91 | trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), extension_loaded(), extension_loaded(), apply_filters() |
$context === false && isset($args) | 85–97 | is_dir(), trailingslashit(), uniqid(), fopen(), fileowner(), fileowner(), fclose(), unlink(), extension_loaded(), extension_loaded(), extension_loaded(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 102 | 24–97 | 16 | |
| 8.5 | 102 | 24–97 | 16 | |
| 8.4 | 102 | 24–97 | 16 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 107 | 24–102 | 16 | |
| 8.2 | 107 | 24–102 | 16 | 9 more instructions than PHP 8.1 |
| 8.1 | 98 | 24–93 | 14 | 3 fewer instructions than PHP 7.4 |
| 7.4 | 101 | 24–95 | 14 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while get_filesystem_method() runs, in this order:
- apply_filters( filesystem_method )filterline 2342 (+71 into the body)
Filters the filesystem method to use.
Uses · 2
- trailingslashit()Appends a trailing slash.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 5
- WP_Customize_Manager::customize_pane_settings()Prints JavaScript settings for parent window.
- WP_Filesystem()Initializes and connects the WordPress Filesystem Abstraction classes.
- WP_REST_Plugins_Controller::is_filesystem_available()Determine if the endpoints are available.
- request_filesystem_credentials()Displays a form to the user to request for their FTP/SSH details in order to connect to the filesystem.
- wp_print_request_filesystem_credentials_modal()Prints the filesystem credentials modal when needed.
Source code
function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) { // Please ensure that this is either 'direct', 'ssh2', 'ftpext', or 'ftpsockets'. $method = defined( 'FS_METHOD' ) ? FS_METHOD : false; if ( ! $context ) { $context = WP_CONTENT_DIR; } // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it. if ( WP_LANG_DIR === $context && ! is_dir( $context ) ) { $context = dirname( $context ); } $context = trailingslashit( $context ); if ( ! $method ) { $temp_file_name = $context . 'temp-write-test-' . str_replace( '.', '-', uniqid( '', true ) ); $temp_handle = @fopen( $temp_file_name, 'w' ); if ( $temp_handle ) { // Attempt to determine the file owner of the WordPress files, and that of newly created files. $wp_file_owner = false; $temp_file_owner = false; if ( function_exists( 'fileowner' ) ) { $wp_file_owner = @fileowner( __FILE__ ); $temp_file_owner = @fileowner( $temp_file_name ); } if ( false !== $wp_file_owner && $wp_file_owner === $temp_file_owner ) { /* * WordPress is creating files as the same owner as the WordPress files, * this means it's safe to modify & create new files via PHP. */ $method = 'direct'; $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; } elseif ( $allow_relaxed_file_ownership ) { /* * The $context directory is writable, and $allow_relaxed_file_ownership is set, * this means we can modify files safely in this directory. * This mode doesn't create new files, only alter existing ones. */ $method = 'direct'; $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; } fclose( $temp_handle ); @unlink( $temp_file_name ); } } if ( ! $method && isset( $args['connection_type'] ) && 'ssh' === $args['connection_type'] && extension_loaded( 'ssh2' ) ) { $method = 'ssh2'; } if ( ! $method && extension_loaded( 'ftp' ) ) { $method = 'ftpext'; } if ( ! $method && ( extension_loaded( 'sockets' ) || function_exists( 'fsockopen' ) ) ) { $method = 'ftpsockets'; // Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread. } /** * Filters the filesystem method to use. * * @since 2.6.0 * * @param string $method Filesystem method to return. * @param array $args An array of connection details for the method. * @param string $context Full path to the directory that is tested for being writable. * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. */ return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );}Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-admin/includes/file.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.