3

I'm using wp_handle_upload to allow users upload .csv files in the front end and it's working fine. I was wondering how can I limit this to only allow .csv files though since currently it accepts a wide variety of file types. According to the doc this should be possible by overriding the $overrides param but I'm not sure what should I pass it to do so.

Thanks in advance!

2 Answers 2

8

Got it, looking at the source code I came up with this:

wp_handle_upload($file_input, array('test_form' => false, 'mimes' => array('csv' => 'text/csv')));

To override the mime types just pass mimes as an array wit the key being the file extension and the value as the mime type.

3
2

The filter you'll want to use is 'upload_mimes' http://xref.yoast.com/trunk/_functions/get_allowed_mime_types.html

The function get_allowed_mime_types gets the filtered $mimes array, so if you want to ONLY allow csv uploads, you can do this:

add_filter('upload_mimes', 'javiervd_filter_mime_types');
function javiervd_filter_mime_types($mimes)
{
  return array('csv' => 'text/csv');
}

Normally with a filter, you'd want to alter the input and return it, but since you only want .csv uploads, you can simply return an array with the one element. It's important to note this will override permitted upload types across the site.

UPDATE: Ok, here's what you can do. I'm assuming the user is logged out, and that's a good way to test that this is the kind of upload where only csvs are permitted. If it's not, you can always just modify your if check and make sure the filter only gets applied to front-end uploads.

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;
}  
4
  • But won't that affect all instances of wp_handle_upload ? Even in the WP backend? Dec 24, 2011 at 6:48
  • Whoops, we crossed the streams. Where did you want it specifically overridden? Dec 24, 2011 at 6:49
  • Edit: A couple of questions. Are the users who are uploading the files logged in at all? If not, is it a guarantee that they are NOT logged in? Dec 24, 2011 at 6:55
  • Well they can be logged in or out that's why I wasn't sure about how to approach this, I know there's a way to do it with the $overrides param because in the source code of the wp_handle_upload function it says // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter. So in theory I can use either the filter or the override. Dec 24, 2011 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.