wp_filesystem

函数
wp_filesystem ( $args = false, $context = false, $allow_relaxed_file_ownership = false )
参数
  • (array|false) $args Optional. Connection args, These are passed directly to the `WP_Filesystem_*()` classes. Default false.
    Required:
    Default: false
  • (string|false) $context Optional. Context for get_filesystem_method(). Default false.
    Required:
    Default: false
  • (bool) $allow_relaxed_file_ownership Optional. Whether to allow Group/World writable. Default false.
    Required:
    Default: false
返回值
  • (bool|null) True on success, false on failure, null if the filesystem method class file does not exist.
定义位置
相关方法
wp_filesizewp_is_streamget_filesystem_methodwp_title_rsswp_check_filetype
引入
2.5.0
弃用
-

wp_filesystem。这个类为与WordPress中的文件系统进行交互提供了一个抽象层。它为处理文件和目录提供了一个一致的接口,无论底层的文件系统是什么。

初始化并连接WordPress文件系统抽象类。

这个函数将包括所选择的传输并尝试连接。

插件可以添加额外的运输工具,并通过{@see ‘filesystem_method_file’}过滤器返回文件名来强制WordPress使用它们。

function WP_Filesystem( $args = false, $context = false, $allow_relaxed_file_ownership = false ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	global $wp_filesystem;

	require_once ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php';

	$method = get_filesystem_method( $args, $context, $allow_relaxed_file_ownership );

	if ( ! $method ) {
		return false;
	}

	if ( ! class_exists( "WP_Filesystem_$method" ) ) {

		/**
		 * Filters the path for a specific filesystem method class file.
		 *
		 * @since 2.6.0
		 *
		 * @see get_filesystem_method()
		 *
		 * @param string $path   Path to the specific filesystem method class file.
		 * @param string $method The filesystem method to use.
		 */
		$abstraction_file = apply_filters( 'filesystem_method_file', ABSPATH . 'wp-admin/includes/class-wp-filesystem-' . $method . '.php', $method );

		if ( ! file_exists( $abstraction_file ) ) {
			return;
		}

		require_once $abstraction_file;
	}
	$method = "WP_Filesystem_$method";

	$wp_filesystem = new $method( $args );

	/*
	 * Define the timeouts for the connections. Only available after the constructor is called
	 * to allow for per-transport overriding of the default.
	 */
	if ( ! defined( 'FS_CONNECT_TIMEOUT' ) ) {
		define( 'FS_CONNECT_TIMEOUT', 30 ); // 30 seconds.
	}
	if ( ! defined( 'FS_TIMEOUT' ) ) {
		define( 'FS_TIMEOUT', 30 ); // 30 seconds.
	}

	if ( is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->has_errors() ) {
		return false;
	}

	if ( ! $wp_filesystem->connect() ) {
		return false; // There was an error connecting to the server.
	}

	// Set the permission constants if not already set.
	if ( ! defined( 'FS_CHMOD_DIR' ) ) {
		define( 'FS_CHMOD_DIR', ( fileperms( ABSPATH ) & 0777 | 0755 ) );
	}
	if ( ! defined( 'FS_CHMOD_FILE' ) ) {
		define( 'FS_CHMOD_FILE', ( fileperms( ABSPATH . 'index.php' ) & 0777 | 0644 ) );
	}

	return true;
}

常见问题

FAQs
查看更多 >