源代码
File: wp-includes/functions.php
function _wp_upload_dir( $time = null ) {
$siteurl = get_option( 'siteurl' );
$upload_path = trim( get_option( 'upload_path' ) );
if ( empty( $upload_path ) || 'wp-content/uploads' == $upload_path ) {
$dir = WP_CONTENT_DIR . '/uploads';
} elseif ( 0 !== strpos( $upload_path, ABSPATH ) ) {
// $dir is absolute, $upload_path is (maybe) relative to ABSPATH
$dir = path_join( ABSPATH, $upload_path );
} else {
$dir = $upload_path;
}
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;
}
/*
* Honor the value of UPLOADS. This happens as long as ms-files rewriting is disabled.
* We also sometimes obey UPLOADS when rewriting is enabled -- see the next block.
*/
if ( defined( 'UPLOADS' ) && ! ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) ) {
$dir = ABSPATH . UPLOADS;
$url = trailingslashit( $siteurl ) . UPLOADS;
}
// If multisite (and if not the main site in a post-MU network)
if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
if ( ! get_site_option( 'ms_files_rewriting' ) ) {
/*
* If ms-files rewriting is disabled (networks created post-3.5), it is fairly
* straightforward: Append sites/%d if we're not on the main site (for post-MU
* networks). (The extra directory prevents a four-digit ID from conflicting with
* a year-based directory for the main site. But if a MU-era network has disabled
* ms-files rewriting manually, they don't need the extra directory, as they never
* had wp-content/uploads for the main site.)
*/
if ( defined( 'MULTISITE' ) )
$ms_dir = '/sites/' . get_current_blog_id();
else
$ms_dir = '/' . get_current_blog_id();
$dir .= $ms_dir;
$url .= $ms_dir;
} elseif ( defined( 'UPLOADS' ) && ! ms_is_switched() ) {
/*
* Handle the old-form ms-files.php rewriting if the network still has that enabled.
* When ms-files rewriting is enabled, then we only listen to UPLOADS when:
* 1) We are not on the main site in a post-MU network, as wp-content/uploads is used
* there, and
* 2) We are not switched, as ms_upload_constants() hardcodes these constants to reflect
* the original blog ID.
*
* Rather than UPLOADS, we actually use BLOGUPLOADDIR if it is set, as it is absolute.
* (And it will be set, see ms_upload_constants().) Otherwise, UPLOADS can be used, as
* as it is relative to ABSPATH. For the final piece: when UPLOADS is used with ms-files
* rewriting in multisite, the resulting URL is /files. (#WP22702 for background.)
*/
if ( defined( 'BLOGUPLOADDIR' ) )
$dir = untrailingslashit( BLOGUPLOADDIR );
else
$dir = ABSPATH . UPLOADS;
$url = trailingslashit( $siteurl ) . 'files';
}
}
$basedir = $dir;
$baseurl = $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;
return array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $basedir,
'baseurl' => $baseurl,
'error' => false,
);
}
更新日志
Version | 描述 |
---|---|
4.5.0 | Introduced. |
在WordPress中,wp_upload_dir()
函数是用来获取与上传文件相关的目录路径和URL。这个函数非常有用,尤其是在你需要在WordPress中处理文件上传时。
以下是 wp_upload_dir()
函数的基本用法:
$upload_dir = wp_upload_dir();
调用这个函数会返回一个关联数组,该数组包含了以下信息:
path
:上传文件夹的绝对路径。url
:上传文件夹的URL。subdir
:相对于上传文件夹的子目录路径(如果有)。basedir
:上传文件夹的基路径,不包含子目录。baseurl
:上传文件夹的基URL,不包含子目录。error
:如果有错误发生,这里会包含错误信息。
使用wp_upload_dir()
的步骤:
- 调用函数:首先,调用
wp_upload_dir()
函数。 - 检查错误:检查返回数组中的
error
键,以确保没有错误发生。 - 使用路径和URL:使用返回的路径和URL来处理文件上传、显示或链接到上传的文件。
下面是一个使用wp_upload_dir()
函数的例子:
<?php
// 获取上传目录信息
$upload_dir = wp_upload_dir();
// 检查是否有错误
if ( !empty( $upload_dir['error'] ) ) {
// 处理错误,例如记录日志或显示错误信息
echo 'Error: ' . $upload_dir['error'];
} else {
// 使用上传目录的路径和URL
$upload_path = $upload_dir['path'];
$upload_url = $upload_dir['url'];
// 假设我们要上传一个文件
$file_name = 'my-file-' . time() . '.txt';
$file_path = $upload_path . '/' . $file_name;
// 将内容写入文件
file_put_contents( $file_path, 'This is a test file.' );
// 输出文件的URL
echo 'File uploaded to: ' . $upload_url . '/' . $file_name;
}
?>
在这个例子中,我们首先获取了上传目录的信息,并检查是否有错误。如果没有错误,我们创建了一个新文件,并将其内容写入到上传目录中。然后,我们输出了文件的URL,以便用户可以访问它。
请记住,处理文件上传时,你需要确保有适当的权限和安全性措施,例如验证上传的文件类型和大小,以及避免目录遍历攻击等。
未经允许不得转载:445IT之家 » WordPress函数_wp_upload_dir()用法