So in PHP, I am checking a file extension against multiple types to get the general type of file
so here is what I have for images alone:
if ( $ext == "bmp" || $ext == "jpg" || $ext == "png" || $ext == "psd" || $ext =="psp" || $ext == "thm" || $ext == "tif" || $ext == "ai" || $ext == "drw" || $ext开发者_运维技巧 == "eps" || $ext == "ps" || $ext == "svg" || $ext == "3dm" || $ext == "dwg" || $ext == "dxf" || $ext == "pln" )
I know that is crazy unefficient, considering the number of if-elses I'll also need to use. Is there another way of doing this to check files, or maybe a better, premade function (like apaches mime_Magic)? thanks for the help!
To solve just this problem, you can do:
if(in_array($ext, $extensions))
and put those extensions in an array. Be sure you're checking the actual mimetype of the file, as well, rather than just blindly trusting the extension.
Use something like this
$extensions = array("bmp", "jpg", "png", "psd", "psp", "thm", "tif", "ai", "drw", "eps", "ps", "svg", "3dm", "dwg", "dxf", "pln");
if (in_array($ext, $extensions))
Mime content type, Fileinfo functions
preg_match("/bmp|jpg|png|psd|psp|thm|tif|ai|drw|eps|ps|svg|3dm|dwg|dxf|pln/",$ext,$matches);
print $matches[1];
精彩评论