更改PDF文件的上载目录

时间:2012-03-30 作者:Sinklar

我想更改PDF文件的上载目录only, 不适用于其他文件。例如,PDF文件将上载到wp-content/uploads/pdf 但其他文件将保留在wp-content/uploads/yyyy/mm.

我知道可以过滤upload_dir 但我不知道如何处理文件类型/模拟。

谢谢

2 个回复
SO网友:brasofilo

在《Justice Is廉价》(Justice Is廉价)的指导下,我结束了对该插件功能的改编: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;
}

SO网友:NPatel

您可以从wp config更改上载目录。wordpress中的php文件。

定义(\'UPLOADS\',\'myimages\');确保在行之前添加此代码:

require\\u once(ABSPATH\'wp-settings.php\');

这允许您在myimages文件夹中上载媒体,而不是wp内容/上载

谢谢

结束