我正在使用wp_handle_upload
允许用户上载.csv
文件在前端,工作正常。我想知道我怎样才能将此限制为只允许.csv
文件,但目前它接受各种各样的文件类型。根据文档,这应该可以通过覆盖$overrides
param,但我不确定我应该通过它来做什么。
提前感谢!
我正在使用wp_handle_upload
允许用户上载.csv
文件在前端,工作正常。我想知道我怎样才能将此限制为只允许.csv
文件,但目前它接受各种各样的文件类型。根据文档,这应该可以通过覆盖$overrides
param,但我不确定我应该通过它来做什么。
提前感谢!
明白了,看看我想到的源代码:
wp_handle_upload($file_input, array(\'test_form\' => false, \'mimes\' => array(\'csv\' => \'text/csv\')));
要覆盖mime类型,只需通过mimes
作为一个数组,键是文件扩展名,值是mime类型。您要使用的筛选器是“upload\\u mimes”http://xref.yoast.com/trunk/_functions/get_allowed_mime_types.html
函数get\\u allowed\\u mime\\u types获取过滤后的$mimes数组,因此如果只允许csv上载,可以执行以下操作:
add_filter(\'upload_mimes\', \'javiervd_filter_mime_types\');
function javiervd_filter_mime_types($mimes)
{
return array(\'csv\' => \'text/csv\');
}
通常使用过滤器时,您希望更改输入并返回它,但因为您只需要。csv上传时,您只需返回一个包含一个元素的数组。需要注意的是,这将覆盖整个站点允许的上载类型。更新:好的,下面是你可以做的。我假设用户已注销,这是一种很好的方法,可以测试这种上传是否只允许CSV。如果不是,您总是可以修改If检查,并确保过滤器只应用于前端上传。
add_filter(\'wp_handle_upload_prefilter\' \'javiervd_maybe_filter_mimes\');
function javiervd_maybe_filter_mimes($file)
{
//if not logged in, limit uploads to csvs
if(!is_user_logged_in())
{
add_filter(\'upload_mimes\', \'javiervd_filter_mime_types\');
//add another filter to remove the mime filter so it only applies for the one function call
add_filter(\'wp_handle_upload\', \'javierd_remove_mime_filter\');
}
}
function javiervd_filter_mime_types($mimes)
{
return array(\'csv\' => \'text/csv\');
}
function javiervd_remove_mime_filter($upload)
{
remove_filter(\'upload_mimes\', \'javiervd_filter_mime_types\');
return $upload;
}
我在获取图像时获得404状态,http仍然包含该图像。图像显示在浏览器中,但404代码中断了一些应用程序。对wp内容/上载/的调用被重定向到。htaccess:<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\\.php$ - [L] RewriteRule (.*) /index.php?getfile=$1 [L] </IfModule>&