返回介绍

get_filesystem_method()

发布于 2017-09-10 23:20:44 字数 5014 浏览 949 评论 0 收藏 0

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

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’.


参数

$args

(array) (Optional) Connection details.

Default value: array()

$context

(string) (Optional) Full path to the directory that is tested for being writable.

Default value: ''

$allow_relaxed_file_ownership

(bool) (Optional) Whether to allow Group/World writable.

Default value: false


返回值

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


源代码

File: wp-admin/includes/file.php

function get_filesystem_method( $args = array(), $context = '', $allow_relaxed_file_ownership = false ) {
	$method = defined('FS_METHOD') ? FS_METHOD : false; // Please ensure that this is either 'direct', 'ssh2', 'ftpext' or 'ftpsockets'

	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-' . time();
		$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 = $temp_file_owner = false;
			if ( function_exists('fileowner') ) {
				$wp_file_owner = @fileowner( __FILE__ );
				$temp_file_owner = @fileowner( $temp_file_name );
			}

			if ( $wp_file_owner !== false && $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') && function_exists('stream_get_contents') ) $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 );
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/file.php: filesystem_method
  • wp-includes/formatting.php: trailingslashit()
  • wp-includes/plugin.php: apply_filters()

Used By

  • wp-admin/includes/file.php: wp_print_request_filesystem_credentials_modal()
  • wp-admin/includes/file.php: request_filesystem_credentials()
  • wp-admin/includes/file.php: WP_Filesystem()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文