get_filesystem_method

Determines which method to use for reading, writing, modifying, or deleting files on the filesystem.

Description

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

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 .

Returns (string)

The transport to use, see description for valid return values.

Parameters (3)

0. $args — Optional. (array) => array()
Connection details. Default empty array.
1. $context — Optional. (string) => ''
Full path to the directory that is tested for being writable. Default empty.
2. $allow_relaxed_file_ownership — Optional. (bool) => false
Whether to allow Group/World writable. Default false.

Usage

  1. if ( !function_exists( 'get_filesystem_method' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/file.php'; 
  3.  
  4. // Optional. Connection details. Default empty array. 
  5. $args = array(); 
  6.  
  7. // Optional. Full path to the directory that is tested 
  8. // for being writable. Default empty. 
  9. $context = ''; 
  10.  
  11. // Optional. Whether to allow Group/World writable. 
  12. // Default false. 
  13. $allow_relaxed_file_ownership = false; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_filesystem_method($args, $context, $allow_relaxed_file_ownership); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/file.php  
  1. function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) { 
  2. $method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets' 
  3.  
  4. if ( ! $context ) { 
  5. $context = WP_CONTENT_DIR
  6.  
  7. // If the directory doesn't exist (wp-content/languages) then use the parent directory as we'll create it. 
  8. if ( WP_LANG_DIR == $context && ! is_dir( $context ) ) { 
  9. $context = dirname( $context ); 
  10.  
  11. $context = trailingslashit( $context ); 
  12.  
  13. if ( ! $method ) { 
  14.  
  15. $temp_file_name = $context . 'temp-write-test-' . time(); 
  16. $temp_handle = @fopen($temp_file_name, 'w'); 
  17. if ( $temp_handle ) { 
  18.  
  19. // Attempt to determine the file owner of the WordPress files, and that of newly created files 
  20. $wp_file_owner = $temp_file_owner = false; 
  21. if ( function_exists('fileowner') ) { 
  22. $wp_file_owner = @fileowner( __FILE__ ); 
  23. $temp_file_owner = @fileowner( $temp_file_name ); 
  24.  
  25. if ( $wp_file_owner !== false && $wp_file_owner === $temp_file_owner ) { 
  26. // WordPress is creating files as the same owner as the WordPress files,  
  27. // this means it's safe to modify & create new files via PHP. 
  28. $method = 'direct'; 
  29. $GLOBALS['_wp_filesystem_direct_method'] = 'file_owner'; 
  30. } elseif ( $allow_relaxed_file_ownership ) { 
  31. // The $context directory is writable, and $allow_relaxed_file_ownership is set, this means we can modify files 
  32. // safely in this directory. This mode doesn't create new files, only alter existing ones. 
  33. $method = 'direct'; 
  34. $GLOBALS['_wp_filesystem_direct_method'] = 'relaxed_ownership'; 
  35.  
  36. @fclose($temp_handle); 
  37. @unlink($temp_file_name); 
  38.  
  39. if ( ! $method && isset($args['connection_type']) && 'ssh' == $args['connection_type'] && extension_loaded('ssh2') && function_exists('stream_get_contents') ) $method = 'ssh2'; 
  40. if ( ! $method && extension_loaded('ftp') ) $method = 'ftpext'; 
  41. if ( ! $method && ( extension_loaded('sockets') || function_exists('fsockopen') ) ) $method = 'ftpsockets'; //Sockets: Socket extension; PHP Mode: FSockopen / fwrite / fread 
  42.  
  43. /** 
  44. * Filters the filesystem method to use. 
  45. * @since 2.6.0 
  46. * @param string $method Filesystem method to return. 
  47. * @param array $args An array of connection details for the method. 
  48. * @param string $context Full path to the directory that is tested for being writable. 
  49. * @param bool $allow_relaxed_file_ownership Whether to allow Group/World writable. 
  50. */ 
  51. return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );