开发者

PHP vertify against multiple criteria (more like I don't think this very long if statement is good.. code effiecentcy help!!)

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜