Foreach Loop for Wordpress Upload mimes
Trying to set a loop for the upload mimes in Wordpress. I have an CMS option with a comma delimited list (option_file_types) where a user would specify a list of files types that can be uploaded. But I can't figure out how to get them all put in a foreach and output correctly. It works with one file type entry when not in the foreach. Any help would be hugely appreciated.
Code:
function custom_upload_mimes ($existing开发者_开发问答_mimes = array()) {
$file_types = get_option('option_file_types');
$array = $file_types;
$variables = explode(", ", $array);
foreach($variables as $value) {
$existing_mimes[''.$value.''] = 'mime/type']);
}
return $existing_mimes;
}
Intended output:
$existing_mimes['type'] = 'mime/type';
$existing_mimes['type'] = 'mime/type';
$existing_mimes['type'] = 'mime/type';
function custom_upload_mimes ($existing_mimes = array()) {
$file_types = get_option('option_file_types');
$variables = explode(',', $file_types);
foreach($variables as $value) {
$value = trim($value);
$existing_mimes[$value] = $value;
}
return $existing_mimes;
}
If your $file_types
does not contain mime types but file extensions as your comment suggests then you will also need to convert the file extension to a mime type. A class like this one will help you to convert the extension into a proper mime type.
For example:
require_once 'mimetype.php'; // http://www.phpclasses.org/browse/file/2743.html
function custom_upload_mimes ($existing_mimes = array()) {
$mimetype = new mimetype();
$file_types = get_option('option_file_types');
$variables = explode(',', $file_types);
foreach($variables as $value) {
$value = trim($value);
if(!strstr($value, '/')) {
// if there is no forward slash then this is not a proper
// mime type so we should attempt to find the mime type
// from the extension (eg. xlsx, doc, pdf)
$mime = $mimetype->privFindType($value);
} else {
$mime = $value;
}
$existing_mimes[$value] = $mime;
}
return $existing_mimes;
}
精彩评论