Function Reference/wp upload dir

来自WordPress中文文档
跳转至: 导航搜索

Description

Returns an array of key => value pairs containing path information on the currently configured uploads directory.

Checks the 'upload_path' option, which should be from the web root folder, and if it isn't empty it will be used. If it is empty, then the path will be 'WP_CONTENT_DIR/uploads'. If the 'UPLOADS' constant is defined, then it will override the 'upload_path' option and 'WP_CONTENT_DIR/uploads' path.

The upload URL path is set either by the 'upload_url_path' option or by using the 'WP_CONTENT_URL' constant and appending '/uploads' to the path.

If the 'uploads_use_yearmonth_folders' is set to true (checkbox if checked in the administration settings panel), then the time will be used. The format will be year first and then month.

If the path couldn't be created, then an error will be returned with the key 'error' containing the error message. The error suggests that the parent directory is not writable by the server.

On success, the returned array will have many indices:

* '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.

Usage

<?php $upload_dir = wp_upload_dir(); ?>

Parameters

tt$time/tt
(string) (optional) Time formatted in 'yyyy/mm'.
Default: nullnoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Example

Basic example to produce the upload directory URL.

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

More in-depth break down of the data returned.

<?php 
$uploads = wp_upload_dir(); // Array of key => value pairs
/*
	$uploads 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 $uploads['path'] . '<br />';
echo $uploads['url'] . '<br />';
echo $uploads['subdir'] . '<br />';
echo $uploads['basedir'] . '<br />';
echo $uploads['baseurl'] . '<br />';
echo $uploads['error'] . '<br />';


$upload_dir = ( $uploads['url'] );
$upload_dir_alt = ( $uploads['baseurl'] . $uploads['subdir'] );

// Now echo the final result
echo $upload_dir . '<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_dir_alt . '<br />'; // Output - http://example.com/wp-content/uploads/2010/05 
?>

Important Note

Note that using this function will create a subfolder in your Uploads folder corresponding to the queried month (or current month, if no $time argument is provided), if that folder is not already there. You don't have to upload anything in order for this folder to be created.

Folder Name

In case you want to move the /uploads folder, you'll have to use the UPLOADS constant. It normally shouldn't get used, as it only get's defined when ms_default_constants() is run (only multisite), but you can simply set

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

in a single site install and it will just work, as the public directory structure function wp_upload_dir() sets it up, when it was defined: $dir = ABSPATH . UPLOADS;.

Note: You can extract the folder name with the following line:

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

Change Log

Since: 2.0.0

Source File

wp_upload_dir() is located in onlyincludecodewp-includes/functions.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Related

模板:Directory URL Tags includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude