如何为每种媒体类型设置上传目录?

时间:2012-03-07 作者:Mohamed Said

有没有办法为每种媒体类型设置自定义上载文件夹?例如:。png应保存在上传/png中。上传中的GIF/GIF

1 个回复
SO网友:Max Yudin

[More info added]. See below the code.

//~ change upload dir
add_filter(\'upload_dir\', \'choose_upload_dir\');

//~ upload image
$upload = wp_handle_upload( $_FILES[\'your_form_input_name\'], array(\'test_form\' => false) );

//~ change upload dir back
remove_filter(\'upload_dir\', \'choose_upload_dir\');

function choose_upload_dir($upload) {
    //~ your file to upload
    $file_type = $_FILES[\'your_form_input_name\'][\'type\'];

    //~ switch between file types
    switch($file_type) {
        //~ if type is \'png\' then upload subdir is \'/png\' and so on
        case \'image/png\':
            $type_dir = \'/png\';
        break;

        case \'image/jpeg\':
            $type_dir = \'/jpg\';
        break;

        default:
            $type_dir = \'/others\';
    }

    $upload[\'subdir\'] = $upload[\'subdir\'].$type_dir;
    $upload[\'path\'] = $upload[\'basedir\'].$upload[\'subdir\'];
    $upload[\'url\'] = $upload[\'baseurl\'].$upload[\'subdir\'];

    return $upload;
}
?>
More info: 我偶然发现了一个有用的插件,它允许您为上传设置不同的目录:Relocate Upload

结束

相关推荐

404从wp-Content/Uploads/获取图像时

我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&