Using WordPress ‘get_filesystem_method()’ PHP function

The get_filesystem_method() WordPress PHP function determines which method to use for reading, writing, modifying, or deleting files on the filesystem.

Usage

get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false );

Custom example:

Input:

$method = get_filesystem_method( array( 'ftp_base' => '/public_html/' ), ABSPATH, true );
echo $method;

Output:

'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'

Parameters

  • $args (array) – Optional. Connection details. Default: array()
  • $context (string) – Optional. Full path to the directory that is tested for being writable. Default: ''
  • $allow_relaxed_file_ownership (bool) – Optional. Whether to allow Group/World writable. Default: false

More information

See WordPress Developer Resources: get_filesystem_method

Examples

Get the default filesystem method

This example retrieves the default filesystem method without specifying any parameters.

$default_method = get_filesystem_method();
echo $default_method; // 'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'

Specify FTP connection details

This example specifies FTP connection details, including the base path.

$ftp_args = array(
  'ftp_base' => '/public_html/',
  'ftp_user' => 'yourftpuser',
  'ftp_pass' => 'yourftppassword',
);
$ftp_method = get_filesystem_method( $ftp_args );
echo $ftp_method; // 'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'

Specify a custom context

This example specifies a custom context (full path to the directory) to test for being writable.

$custom_context = '/var/www/html/wp-content/uploads/';
$context_method = get_filesystem_method( array(), $custom_context );
echo $context_method; // 'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'

Allow relaxed file ownership

This example allows Group/World writable file ownership.

$relaxed_file_ownership = get_filesystem_method( array(), '', true );
echo $relaxed_file_ownership; // 'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'

Combining all parameters

This example combines all the parameters, including FTP connection details, custom context, and allowing relaxed file ownership.

$combined_args = array(
  'ftp_base' => '/public_html/',
  'ftp_user' => 'yourftpuser',
  'ftp_pass' => 'yourftppassword',
);
$combined_context = '/var/www/html/wp-content/uploads/';
$combined_method = get_filesystem_method( $combined_args, $combined_context, true );
echo $combined_method; // 'direct' or 'ssh2' or 'ftpext' or 'ftpsockets'