This page redirects to an external site: https://developer.wordpress.org/reference/functions/wp_upload_dir/
Languages: English • 中文(简体) • (Add your language)
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.
<?php $upload_dir = wp_upload_dir(); ?>
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
$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
?>
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.
For creating custom folder for users
<?php
global $current_user;
get_currentuserinfo();
$upload_dir = wp_upload_dir();
$user_dirname = $upload_dir['basedir'].'/'.$current_user->user_login;
if( ! file_exists( $user_dirname ) )
wp_mkdir_p( $user_dirname );
?>
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_DIR ) . '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_DIR ), '', untrailingslashit( UPLOADS ) );
}
Since: 2.0.0
wp_upload_dir() is located in wp-includes/functions.php.