wp_upload_dir

函数
wp_upload_dir ( $time = null, $create_dir = true, $refresh_cache = false )
参数
  • (string) $time Optional. Time formatted in 'yyyy/mm'. Default null.
    Required:
    Default: null
  • (bool) $create_dir Optional. Whether to check and create the uploads directory. Default true for backward compatibility.
    Required:
    Default: true
  • (bool) $refresh_cache Optional. Whether to refresh the cache. Default false.
    Required:
    Default: false
返回值
  • (array) { Array of information about the upload directory. @type string $path Base directory and subdirectory or full path to upload directory. @type string $url Base URL and subdirectory or absolute URL to upload directory. @type string $subdir Subdirectory if uploads use year/month folders option is on. @type string $basedir Path without subdir. @type string $baseurl URL path without subdir. @type string|false $error False or error message. }
定义位置
相关方法
_wp_upload_dirwp_get_upload_dirwp_upload_bitswp_preload_dialogswp_max_upload_size
引入
2.0.0
弃用
-

wp_upload_dir: 这个函数返回一个数组,包含当前站点的上传目录的信息。该数组包括上传目录的路径和URL,以及其他信息,如上传的子目录和上传目录的完整路径。

返回一个包含当前上传目录的路径和URL的数组。

检查’upload_path’选项,它应该是来自web根目录,如果它不是空的,它将被使用。如果它是空的,那么路径将是’WP_CONTENT_DIR/uploads’。如果定义了’UPLOADS’常量,那么它将覆盖’upload_path’选项和’WP_CONTENT_DIR/uploads’路径。

上传URL路径由’upload_url_path’选项设置,或者使用’WP_CONTENT_URL’常量并将’/uploads’附加到路径中。

如果’uploads_use_yearmonth_folders’设置为true(如果在管理设置面板中勾选了复选框),那么就会使用时间。其格式将是先年后月。

如果路径不能被创建,那么将返回一个错误,关键是’error’包含错误信息。该错误表明,父目录不能被服务器写入。

function wp_upload_dir( $time = null, $create_dir = true, $refresh_cache = false ) {
	static $cache = array(), $tested_paths = array();

	$key = sprintf( '%d-%s', get_current_blog_id(), (string) $time );

	if ( $refresh_cache || empty( $cache[ $key ] ) ) {
		$cache[ $key ] = _wp_upload_dir( $time );
	}

	/**
	 * Filters the uploads directory data.
	 *
	 * @since 2.0.0
	 *
	 * @param array $uploads {
	 *     Array of information about the upload directory.
	 *
	 *     @type string       $path    Base directory and subdirectory or full path to upload directory.
	 *     @type string       $url     Base URL and subdirectory or absolute URL to upload directory.
	 *     @type string       $subdir  Subdirectory if uploads use year/month folders option is on.
	 *     @type string       $basedir Path without subdir.
	 *     @type string       $baseurl URL path without subdir.
	 *     @type string|false $error   False or error message.
	 * }
	 */
	$uploads = apply_filters( 'upload_dir', $cache[ $key ] );

	if ( $create_dir ) {
		$path = $uploads['path'];

		if ( array_key_exists( $path, $tested_paths ) ) {
			$uploads['error'] = $tested_paths[ $path ];
		} else {
			if ( ! wp_mkdir_p( $path ) ) {
				if ( 0 === strpos( $uploads['basedir'], ABSPATH ) ) {
					$error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
				} else {
					$error_path = wp_basename( $uploads['basedir'] ) . $uploads['subdir'];
				}

				$uploads['error'] = sprintf(
					/* translators: %s: Directory path. */
					__( 'Unable to create directory %s. Is its parent directory writable by the server?' ),
					esc_html( $error_path )
				);
			}

			$tested_paths[ $path ] = $uploads['error'];
		}
	}

	return $uploads;
}

常见问题

FAQs
查看更多 >