wp_upload_dir

Definition:
function wp_upload_dir( $time = null ) {}

Get an array containing the current upload directory’s path and url.
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.

Parameters

  • string $time: Optional. Time formatted in ‘yyyy/mm’.

Return values

returns:See above for description.

Defined filters

  • upload_dir
    apply_filters( 'upload_dir', array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false )

Source code

function wp_upload_dir( $time = null ) {

	global $switched;

	$siteurl = get_option( 'siteurl' );

	$upload_path = get_option( 'upload_path' );

	$upload_path = trim($upload_path);

	$main_override = is_multisite() && defined( 'MULTISITE' ) && is_main_site();

	if ( empty($upload_path) ) {

		$dir = WP_CONTENT_DIR . '/uploads';

	} else {

		$dir = $upload_path;

		if ( 'wp-content/uploads' == $upload_path ) {

			$dir = WP_CONTENT_DIR . '/uploads';

		} elseif ( 0 !== strpos($dir, ABSPATH) ) {

			// $dir is absolute, $upload_path is (maybe) relative to ABSPATH

			$dir = path_join( ABSPATH, $dir );

		}

	}



	if ( !$url = get_option( 'upload_url_path' ) ) {

		if ( empty($upload_path) || ( 'wp-content/uploads' == $upload_path ) || ( $upload_path == $dir ) )

			$url = WP_CONTENT_URL . '/uploads';

		else

			$url = trailingslashit( $siteurl ) . $upload_path;

	}



	if ( defined('UPLOADS') && !$main_override && ( !isset( $switched ) || $switched === false ) ) {

		$dir = ABSPATH . UPLOADS;

		$url = trailingslashit( $siteurl ) . UPLOADS;

	}



	if ( is_multisite() && !$main_override && ( !isset( $switched ) || $switched === false ) ) {

		if ( defined( 'BLOGUPLOADDIR' ) )

			$dir = untrailingslashit(BLOGUPLOADDIR);

		$url = str_replace( UPLOADS, 'files', $url );

	}



	$bdir = $dir;

	$burl = $url;



	$subdir = '';

	if ( get_option( 'uploads_use_yearmonth_folders' ) ) {

		// Generate the yearly and monthly dirs

		if ( !$time )

			$time = current_time( 'mysql' );

		$y = substr( $time, 0, 4 );

		$m = substr( $time, 5, 2 );

		$subdir = "/$y/$m";

	}



	$dir .= $subdir;

	$url .= $subdir;



	$uploads = apply_filters( 'upload_dir', array( 'path' => $dir, 'url' => $url, 'subdir' => $subdir, 'basedir' => $bdir, 'baseurl' => $burl, 'error' => false ) );



	// Make sure we have an uploads dir

	if ( ! wp_mkdir_p( $uploads['path'] ) ) {

		$message = sprintf( __( 'Unable to create directory %s. Is its parent directory writable by the server?' ), $uploads['path'] );

		return array( 'error' => $message );

	}



	return $uploads;

}

4251

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: