問題描述
我想只更改 PDF 檔案的上傳目錄,而不是其他檔案。例如,PDF 檔案將被上傳到 wp-content/uploads/pdf,但其他檔案將保留在 wp-content/uploads/yyyy/mm 中。
我知道可以過濾 upload_dir,但我不知道如何使用檔案型別/mimes 。
謝謝!
最佳解決方案
正義是廉價的領導,我結束了從這個外掛的功能:http://wordpress.org/extend/plugins/custom-upload-dir/
<?php
/*
* Change upload directory for PDF files
* Only works in WordPress 3.3+
*/
add_filter('wp_handle_upload_prefilter', 'wpse47415_pre_upload');
add_filter('wp_handle_upload', 'wpse47415_post_upload');
function wpse47415_pre_upload($file){
add_filter('upload_dir', 'wpse47415_custom_upload_dir');
return $file;
}
function wpse47415_post_upload($fileinfo){
remove_filter('upload_dir', 'wpse47415_custom_upload_dir');
return $fileinfo;
}
function wpse47415_custom_upload_dir($path){
$extension = substr(strrchr($_POST['name'],'.'),1);
if(!empty($path['error']) || $extension != 'pdf') { return $path; } //error or other filetype; do nothing.
$customdir = '/pdf';
$path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。