当前浏览:首页 / WordPress函数 / wp_upload_dir()

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

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

dir


描述

检查‘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”。该错误表明服务器无法写入父目录。


参数

$time

(string)(可选) 时间格式为'yyyy/mm'。

默认值: null

$create_dir

(bool)(可选) 是否检查并创建上传目录。向后兼容性的默认值为true。

默认值: true

$refresh_cache

(bool)(可选) 是否刷新缓存。

默认值: false


返回

(array) 有关上载目录的信息数组。

  • 'path'
    (string) 基本目录和子目录或上传目录的完整路径。
  • 'url'
    (string) 基本URL和子目录或上载目录的绝对URL。
  • 'subdir'
    (string) 如果启用了“以年—月目录形式组织上传内容”选项,则返回子目录。
  • 'basedir'
    (string) 没有子目录的路径。
  • 'baseurl'
    (string) 没有子目录的URL路径。
  • 'error'
    (string|false) False或错误消息。

更多信息

请注意,使用此函数将在Uploads文件夹中创建一个子文件夹,该子文件夹对应于查询的月份(或当前月份,如果未提供$time参数),如果该文件夹尚未存在。要创建此文件夹,您无需上传任何内容。

用于为用户创建自定义文件夹

$current_user = wp_get_current_user();
$upload_dir   = wp_upload_dir();

if ( isset( $current_user->user_login ) && ! empty( $upload_dir['basedir'] ) ) {
    $user_dirname = $upload_dir['basedir'].'/'.$current_user->user_login;
        if ( ! file_exists( $user_dirname ) ) {
	    wp_mkdir_p( $user_dirname );
    }
}

文件夹名称

如果要移动/uploads文件夹,则必须使用UPLOADS常量。通常不应该使用它,因为只有在运行ms_default_constants()时才定义它(仅多站点),但您可以简单地设置:

define( 'UPLOADS', trailingslashit( WP_CONTENT_DIR ) . 'custom_uploads_name' );

在单站点安装中,它将正常工作,因为公共目录结构函数wp_upload_dir()在定义时设置了它:

$dir = ABSPATH . UPLOADS;

注意:您可以使用以下行提取文件夹名称:

// returns `false` if the UPLOADS constant is not defined
$upload_dir_name = false;
if ( defined( 'UPLOADS' ) ) {
	str_replace( trailingslashit( WP_CONTENT_DIR ), '', untrailingslashit( UPLOADS ) );
}


源码

查看源码 官方文档


更新日志

版本描述
2.0.0开始引入

使用示例

  • 示例1

    对返回的数据进行更深入的分解。

    <?php
    $upload_dir = wp_upload_dir(); // Array of key => value pairs
    /*
        $upload_dir now contains something like the following (if successful)
        Array (
            [path] => C:\path\to\wordpress\wp-content\uploads\2010\05
            [url] => http://example.com/wp-content/uploads/2010/05
            [subdir] => /2010/05
            [basedir] => C:\path\to\wordpress\wp-content\uploads
            [baseurl] => http://example.com/wp-content/uploads
            [error] =>
        )
        // Descriptions
        [path] - base directory and sub directory or full path to upload directory.
        [url] - base url and sub directory or absolute URL to upload directory.
        [subdir] - sub directory if uploads use year/month folders option is on.
        [basedir] - path without subdir.
        [baseurl] - URL path without subdir.
        [error] - set to false.
    */
    
    echo $upload_dir['path'] . '<br />';
    echo $upload_dir['url'] . '<br />';
    echo $upload_dir['subdir'] . '<br />';
    echo $upload_dir['basedir'] . '<br />';
    echo $upload_dir['baseurl'] . '<br />';
    echo $upload_dir['error'] . '<br />';
    
    $upload_url = ( $upload_dir['url'] );
    $upload_url_alt = ( $upload_dir['baseurl'] . $upload_dir['subdir'] );
    
    // Now echo the final result
    echo $upload_url . '<br />'; // Output - http://example.com/wp-content/uploads/2010/05
    
    // Using year and month based folders, the below will be the same as the line above.
    echo $upload_url_alt . '<br />'; // Output - http://example.com/wp-content/uploads/2010/05
    ?>
    
    
  • 示例2

    奇怪的是wp_upload_dir没有为SSL网站返回https。希望他们能尽快解决这个问题。下面是代码,您可以在其中以正确的方式获得上传目录和url:

    /**
     * Get the upload URL/path in right way (works with SSL).
     *
     * @param $param string "basedir" or "baseurl"
     * @return string
     */
    function fn_get_upload_dir_var( $param, $subfolder = '' ) {
    	$upload_dir = wp_upload_dir();
    	$url = $upload_dir[ $param ];
    
    	if ( $param === 'baseurl' && is_ssl() ) {
    		$url = str_replace( 'http://', 'https://', $url );
    	}
    
    	return $url . $subfolder;
    }

    例子:

    // Get wp-content/uploads/myfolder by URL
    fn_get_upload_dir_var( 'baseurl', 'myfolder' );
    
    // Now get full path of the same folder
    fn_get_upload_dir_var( 'basedir', 'myfolder' );
  • 示例3

    请注意,上面提到的‘upload_path’选项自WP3.5以来已被删除,因此该检查仅在向后兼容性方面存在。

  • 示例4

    我根本不需要依赖Gravatar,只需要使用buddypress的个人资料头像。结果如下。

    $avatar_url = bp_core_fetch_avatar(array('item_id'=>$user->ID,'html'=>false));        
    if (strpos($avatar_url, 'www.gravatar.com/avatar') !== false) {
    	$upload_dir = wp_upload_dir();
        $avatar_url = $upload_dir['baseurl'] . '/avatars/pi-gravatar.jpg';
    }
    
  • 示例5

    生成上传目录URL的基本示例。

    <?php $upload_dir = wp_upload_dir(); ?>
    <?php echo $upload_dir['baseurl']; ?>
    
  • 示例6

    如果这对任何人都有帮助,在多站点实例中,站点和站点编号将附加到路径的末尾。

    $upload_dir = wp_upload_dir();
    
    // Single Site
    $upload_dir['baseurl']; // http://example.com/wp-content/uploads
    
    // Multisite
    $upload_dir['baseurl']; // http://example.com/wp-content/uploads/sites/site_no
    // Where site_no = Site ID
    
    

    需要注意的是,['subdir']键仍然返回年/月结构,但所有其他相关路径都会在多站点环境中添加上述目录。

  • 示例7

    在这种情况下,它有助于任何人想要使定义常量,移动和重命名多站点上传文件。

    //If planned rename UPLOADS file from wp-content/uploads to wp-content/pics
    //If NOT PLANNED force all uploads shared in one folder
    //Add define constants define ('UPLOADS', WP_CONTENT_DIR . '/' . 'pics'); in .htaccess
    //Sub-directories (like year/month) will still exist (if the network setting is enabled)
    //Register a filter for "upload_dir"
    
    add_filter( 'upload_dir', 'ms_upload_dir' );
    
    function ms_upload_dir( $dirs ) {
        $dirs['baseurl'] = network_site_url( '/wp-content/pics') . '/sites/'.get_current_blog_id();
        $dirs['basedir'] = ABSPATH . 'wp-content/pics' . '/sites/'.get_current_blog_id();
        $dirs['path'] = $dirs['basedir'] . $dirs['subdir'];
        $dirs['url'] = $dirs['baseurl'] . $dirs['subdir'];
    
        return $dirs;
    }
    
    //If planned rename UPLOADS file from wp-content/uploads to wp-content/pics
    //If PLANNED force all uploads shared in one folder
    //Add define constants define ('UPLOADS', WP_CONTENT_DIR . '/' . 'pics'); in .htaccess
    //Sub-directories (like year/month) will still exist (if the network setting is enabled)
    //Register a filter for "upload_dir"
    
    add_filter( 'upload_dir', 'ms_upload_dir' );
    
    function ms_upload_dir( $dirs ) {
        $dirs['baseurl'] = network_site_url( '/wp-content/pics');
        $dirs['basedir'] = ABSPATH . 'wp-content/pics';
        $dirs['path'] = $dirs['basedir'] . $dirs['subdir'];
        $dirs['url'] = $dirs['baseurl'] . $dirs['subdir'];
    
        return $dirs;
    }
    
    //If planned moving (and rename) UPLOADS file out from wp-content
    //If NOT PLANNED force all uploads shared in one folder
    //Add define constants define ('UPLOADS', 'pics'); in .htaccess
    //Sub-directories (like year/month) will still exist (if the network setting is enabled)
    //Register a filter for "upload_dir"
    
    add_filter( 'upload_dir', 'ms_upload_dir' );
    
    function ms_upload_dir( $dirs ) {
        $dirs['baseurl'] = network_site_url( '/pics') . '/sites/'.get_current_blog_id();
        $dirs['basedir'] = ABSPATH . 'pics' . '/sites/'.get_current_blog_id();
        $dirs['path'] = $dirs['basedir'] . $dirs['subdir'];
        $dirs['url'] = $dirs['baseurl'] . $dirs['subdir'];
    
        return $dirs;
    }
    
    //If planned moving (and rename) UPLOADS file out from wp-content
    //If PLANNED force all uploads shared in one folder
    //Add define constants define ('UPLOADS', 'pics'); in .htaccess
    //Sub-directories (like year/month) will still exist (if the network setting is enabled)
    //Register a filter for "upload_dir"
    
    add_filter( 'upload_dir', 'ms_upload_dir' );
    
    function ms_upload_dir( $dirs ) {
        $dirs['baseurl'] = network_site_url( '/pics');
        $dirs['basedir'] = ABSPATH . 'pics';
        $dirs['path'] = $dirs['basedir'] . $dirs['subdir'];
        $dirs['url'] = $dirs['baseurl'] . $dirs['subdir'];
    
        return $dirs;
    }
    
    
  • 示例8

    如果您想在WordPress插件激活时在wp-contents/uploads文件夹中创建一个目录,以下是代码:

    function yourprojectname_activate() {
     
        $upload = wp_upload_dir();
        $upload_dir = $upload['basedir'];
        $upload_dir = $upload_dir . '/your-project-name';
        if (! is_dir($upload_dir)) {
           mkdir( $upload_dir, 0700 );
        }
    }