首頁 CMS教程 &#&按 wordpress上傳的檔案在哪

wordpress上傳的檔案在哪

Jul 17, 2019 am 10:17 AM
wordpress

wordpress上傳的檔案在哪

wordpress上传的文件在哪?

WordPress默认的上传文件的目录是/wp-content/uploads,而且文件是以年月的形式组织的,虽然我们可以去掉按照年月组织的选项,或者更改存储路径,但这个设置会应用到全局,不能按照特定条件选择特定目录存储文件。

WordPress上传默认设置

有时候将不同类型的文件分门别类存储,似乎比年月目录更有意义。例如幻灯片应该存储在slides目录下,下载文件应该存储在downloads文件夹下。就说幻灯片slideshow,我比较喜欢用自定义文章类型(Custom Post Type)实现,有些幻灯片脚本比较个性,不支持绝对路径,必须用相对路径,然后用base参数设置相对于哪个文件夹,这样幻灯片必须存储在某个特定的文件夹中,年月形式显然不满足要求。所以,我们需要条件化的设置上传目录。

为Custom Post Type设置上传目录

假设我要将所有在幻灯片类型的文章中上传的文件存储到/wp-content/uploads/slides文件夹中,将下面的代码放到主题的functions.php中即可

function custom_upload_directory( $uploads ) {
    $id = $_REQUEST['post_id'];
        $parent = get_post( $id )->post_parent;
 
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $subdir = 'slides';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
    }
    return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
登入後複製

将post-type替换成自己的自定义文章类型名称,将你要创建的子目录赋值给$subdir。

将文件保存到插件目录

下面的代码要用在插件中,文件会保存到插件目录下的uploads文件夹下。

/**
 * Change Upload Directory for Custom Post-Type
 *
 * This will change the upload directory for a custom post-type. Attachments will
 * now be uploaded to an "uploads" directory within the folder of your plugin. Make
 * sure you swap out "post-type" in the if-statement with the appropriate value...
 */
function custom_upload_directory( $args ) {
  
    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;
  
    // Check the post-type of the current post
    if( "post-type" == get_post_type( $id ) || "post-type" == get_post_type( $parent ) ) {
        $args['path'] = plugin_dir_path(__FILE__) . "uploads";
        $args['url']  = plugin_dir_url(__FILE__) . "uploads";
        $args['basedir'] = plugin_dir_path(__FILE__) . "uploads";
        $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads";
    }
    return $args;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
登入後複製

如果要以年月形式保存,修改一下代码即可

$args['path'] = plugin_dir_path(__FILE__) . "uploads" . $args['subdir'];
$args['url']  = plugin_dir_url(__FILE__) . "uploads" . $args['subdir'];
登入後複製

这段代码来自 http://wordpress.stackexchange.com/questions/35657/how-to-add-more-upload-directories/

为后台管理页面设定upload_dir

用wp_editor在后台管理页面(比如用add_menu_page创建的页面)创建一个媒体上传功能,希望所有从该页面上传的文件都保存到wp-content/uploads/myfolder目录下。

由 于ajax上传是直接调用wp-admin/async_upload.php文件,只能通过post_id获取post信息,而后台管理页面并非 post,所以判断什么时候应该更改upload_dir有些麻烦。此时,可以用采用判断页面referer的方法,用wp_get_referer() 函数获取引荐url,如果正好与我们的option page url想等,就更该目录。

function custom_upload_directory( $uploads ) {
 
   if( wp_get_referer() == 'http://domain.com/wp-admin/admin.php?page=myoptionpage'){
        $subdir = 'myfolder';
        $uploads['subdir'] = $subdir;
        $uploads['path'] = $uploads['basedir'].DIRECTORY_SEPARATOR.$subdir;
        $uploads['url'] = $uploads['baseurl'].'/'.$subdir;
   }
   return $uploads;
}
add_filter( 'upload_dir', 'custom_upload_directory' );
登入後複製

参考信息

filter:upload_dir是在wp_upload_dir()函数中调用的

$upload_dir = wp_upload_dir();
$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] =>
)
登入後複製

更多WordPress技术文章,请访问WordPress教程栏目!

以上是wordpress上傳的檔案在哪的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

wordpress文章列表怎麼調 wordpress文章列表怎麼調 Apr 20, 2025 am 10:48 AM

有四種方法可以調整 WordPress 文章列表:使用主題選項、使用插件(如 Post Types Order、WP Post List、Boxy Stuff)、使用代碼(在 functions.php 文件中添加設置)或直接修改 WordPress 數據庫。

wordpress主機怎麼建站 wordpress主機怎麼建站 Apr 20, 2025 am 11:12 AM

要使用 WordPress 主機建站,需要:選擇一個可靠的主機提供商。購買一個域名。設置 WordPress 主機帳戶。選擇一個主題。添加頁面和文章。安裝插件。自定義您的網站。發布您的網站。

wordpress屏蔽ip的插件有哪些 wordpress屏蔽ip的插件有哪些 Apr 20, 2025 am 08:27 AM

WordPress 屏蔽 IP 的插件選擇至關重要。可考慮以下類型:基於 .htaccess:高效,但操作複雜;數據庫操作:靈活,但效率較低;基於防火牆:安全性能高,但配置複雜;自行編寫:最高控制權,但需要更多技術水平。

wordpress主題頭部圖片如何更換 wordpress主題頭部圖片如何更換 Apr 20, 2025 am 10:00 AM

更換 WordPress 主題頭部圖片的分步指南:登錄 WordPress 儀錶盤,導航至“外觀”>“主題”。選擇要編輯的主題,然後單擊“自定義”。打開“主題選項”面板並尋找“網站標頭”或“頭部圖片”選項。單擊“選擇圖像”按鈕並上傳新的頭部圖片。裁剪圖像並單擊“保存並裁剪”。單擊“保存並發布”按鈕以更新更改。

wordpress編輯日期怎麼取消 wordpress編輯日期怎麼取消 Apr 20, 2025 am 10:54 AM

WordPress 編輯日期可以通過三種方法取消:1. 安裝 Enable Post Date Disable 插件;2. 在 functions.php 文件中添加代碼;3. 手動編輯 wp_posts 表中的 post_modified 列。

wordpress怎麼寫頁頭 wordpress怎麼寫頁頭 Apr 20, 2025 pm 12:09 PM

在WordPress中創建自定義頁頭的步驟如下:編輯主題文件“header.php”。添加您的網站名稱和描述。創建導航菜單。添加搜索欄。保存更改並查看您的自定義頁頭。

wordpress怎麼導入源碼 wordpress怎麼導入源碼 Apr 20, 2025 am 11:24 AM

導入 WordPress 源碼需要以下步驟:創建子主題以進行主題修改。導入源碼,覆蓋子主題中的文件。激活子主題,使其生效。測試更改,確保一切正常。

wordpress出現錯誤怎麼辦 wordpress出現錯誤怎麼辦 Apr 20, 2025 am 11:57 AM

WordPress 錯誤解決指南:500 內部服務器錯誤:禁用插件或檢查服務器錯誤日誌。 404 未找到頁面:檢查 permalink 並確保頁面鏈接正確。白屏死機:增加服務器 PHP 內存限制。數據庫連接錯誤:檢查數據庫服務器狀態和 WordPress 配置。其他技巧:啟用調試模式、檢查錯誤日誌和尋求支持。預防錯誤:定期更新 WordPress、僅安裝必要插件、定期備份網站和優化網站性能。

See all articles