get_filesystem_method()
云策文档标注
概述
get_filesystem_method() 函数用于确定 WordPress 文件系统操作(如读写、修改或删除文件)应使用的传输方法。它根据服务器环境和配置自动选择优先级最高的可用方法,并支持通过常量或过滤器覆盖。
关键要点
- 函数返回的传输方法包括:'direct'(直接文件系统访问)、'ssh2'(SSH2 扩展)、'ftpext'(FTP PHP 扩展)和 'ftpsockets'(FTP 套接字)。
- 优先级顺序为:Direct > SSH2 > FTP PHP Extension > FTP Sockets,但可通过定义 FS_METHOD 常量或应用 'filesystem_method' 过滤器来覆盖。
- 函数接受三个参数:$args(连接详情数组)、$context(测试可写性的目录路径)和 $allow_relaxed_file_ownership(是否允许宽松文件所有权)。
- 内部逻辑会检查文件所有权和目录可写性,以决定是否使用 'direct' 方法,并支持 SSH2、FTP 扩展和套接字的后备检测。
代码示例
function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
// 核心逻辑:检查 FS_METHOD 常量,测试文件所有权,并检测扩展以确定方法
$method = defined( 'FS_METHOD' ) ? FS_METHOD : false;
// ... 详细检测代码 ...
return apply_filters( 'filesystem_method', $method, $args, $context, $allow_relaxed_file_ownership );
}注意事项
- 在 wp-config.php 中定义 FS_METHOD 常量可以强制指定文件系统方法,例如 define('FS_METHOD', 'ftpext')。
- 使用 'filesystem_method' 过滤器可以动态修改返回的方法,适用于插件或主题自定义场景。
- 函数默认测试 WP_CONTENT_DIR 目录的可写性,但可通过 $context 参数指定其他路径。
原文内容
Determines which method to use for reading, writing, modifying, or deleting files on the filesystem.
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’.
Parameters
$argsarrayoptional-
Connection details.
Default:
array() $contextstringoptional-
Full path to the directory that is tested for being writable. Default empty.
$allow_relaxed_file_ownershipbooloptional-
Whether to allow Group/World writable.
Default:
false
Source
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 );
}
Hooks
- apply_filters( ‘filesystem_method’, string $method, array $args, string $context, bool $allow_relaxed_file_ownership )
-
Filters the filesystem method to use.
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |