获取包含当前上载目录的路径和URL的数组。

原型

wp_upload_dir( string $time = null, bool $create_dir = true, bool $refresh_cache = false )

描述

检查’upload_path’选项,该选项应该来自Web根文件夹,如果它不为空,则将使用它。如果它为空,则路径为’WP_CONTENT_DIR / uploads’。如果定义了’UPLOADS’常量,那么它将覆盖’upload_path’选项和’WP_CONTENT_DIR / uploads’路径。

参数

$time (string) (Optional)  时间格式为’yyyy / mm’。

$create_dir (bool) (Optional)  是否检查并创建uploads目录。默认为true表示向后兼容性。

$refresh_cache (bool) (Optional)  是否刷新缓存。

返回值

(array) Array of information about the upload directory.

'path'
(string) Base directory and subdirectory or full path to upload directory.
'url'
(string) Base URL and subdirectory or absolute URL to upload directory.
'subdir'
(string) Subdirectory if uploads use year/month folders option is on.
'basedir'
(string) Path without subdir.
'baseurl'
(string) URL path without subdir.
'error'
(string|false) False or error message.

源文件

路径:wp-includes/functions.php

<?php
...
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 upload directory data with keys of 'path',
	 *                       'url', 'subdir, 'basedir', and 'error'.
	 */
	$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 = 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;
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_upload_dir/