wp_upload_dir( string $time = null, bool $create_dir = true, bool $refresh_cache = false )
Returns an array containing the current upload directory’s path and URL.
Contents
Description Description
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.
Parameters Parameters
- $time
-
(string) (Optional) Time formatted in 'yyyy/mm'.
Default value: null
- $create_dir
-
(bool) (Optional) Whether to check and create the uploads directory. Default true for backward compatibility.
Default value: true
- $refresh_cache
-
(bool) (Optional) Whether to refresh the cache.
Default value: false
Return Return
(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.
Source Source
File: wp-includes/functions.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 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; }
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.0.0 | Introduced. |
More Information More Information
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
$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 ); } }
Folder Name 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_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 ) ); }
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
More in-depth break down of the data returned.
Expand full source codeCollapse full source code
Strangely wp_upload_dir doesn’t return https for SSL websites. Hopefully they fix the issue soon. Here is the code below where you can get the upload dir and url in right way:
Expand full source codeCollapse full source code
Example:
Feedback
#25449 seems to be the relevant ticket. — By mrwweb —
I needed to NOT rely on Gravatar at all, and just use the buddypress profile avatar. Here is the result.
Note that the ‘upload_path’ option that is mentioned has been removed since WP3.5, so this check is only still there for backwards compatibility.
Basic example to produce the upload directory URL.
If you want to create a directory in wp-contents/uploads folder at the time of WordPress PlugIn activation, here is the code:
In case this helps anyone, on a multisite instance, the site and site number are appended to the end of the paths.
It’s important to note that the [‘subdir’] key still returns the year/month structure, but all other related paths add the above directories in a Multisite environment.