源代碼
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()
函數(shù)是用來獲取與上傳文件相關(guān)的目錄路徑和URL。這個函數(shù)非常有用,尤其是在你需要在WordPress中處理文件上傳時。
以下是 wp_upload_dir()
函數(shù)的基本用法:
$upload_dir = wp_upload_dir();
調(diào)用這個函數(shù)會返回一個關(guān)聯(lián)數(shù)組,該數(shù)組包含了以下信息:
path
:上傳文件夾的絕對路徑。url
:上傳文件夾的URL。subdir
:相對于上傳文件夾的子目錄路徑(如果有)。basedir
:上傳文件夾的基路徑,不包含子目錄。baseurl
:上傳文件夾的基URL,不包含子目錄。error
:如果有錯誤發(fā)生,這里會包含錯誤信息。
使用wp_upload_dir()
的步驟:
- 調(diào)用函數(shù):首先,調(diào)用
wp_upload_dir()
函數(shù)。 - 檢查錯誤:檢查返回數(shù)組中的
error
鍵,以確保沒有錯誤發(fā)生。 - 使用路徑和URL:使用返回的路徑和URL來處理文件上傳、顯示或鏈接到上傳的文件。
下面是一個使用wp_upload_dir()
函數(shù)的例子:
<?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'];
// 假設(shè)我們要上傳一個文件
$file_name = 'my-file-' . time() . '.txt';
$file_path = $upload_path . '/' . $file_name;
// 將內(nèi)容寫入文件
file_put_contents( $file_path, 'This is a test file.' );
// 輸出文件的URL
echo 'File uploaded to: ' . $upload_url . '/' . $file_name;
}
?>
在這個例子中,我們首先獲取了上傳目錄的信息,并檢查是否有錯誤。如果沒有錯誤,我們創(chuàng)建了一個新文件,并將其內(nèi)容寫入到上傳目錄中。然后,我們輸出了文件的URL,以便用戶可以訪問它。
請記住,處理文件上傳時,你需要確保有適當(dāng)?shù)臋?quán)限和安全性措施,例如驗證上傳的文件類型和大小,以及避免目錄遍歷攻擊等。
未經(jīng)允許不得轉(zhuǎn)載:445IT之家 » WordPress函數(shù)_wp_upload_dir()用法