代码之家  ›  专栏  ›  技术社区  ›  Kyle Hotchkiss

php根据多个条件进行验证(更像是我认为if语句很好不会很长时间)。代码效率帮助!!)

php
  •  0
  • Kyle Hotchkiss  · 技术社区  · 15 年前

    因此,在PHP中,我将针对多个类型检查文件扩展名,以获取文件的一般类型

    以下是我的图片:

    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" )
    

    我知道这是疯狂的不正式,考虑到如果埃尔斯的数量,我也需要使用。有没有另一种检查文件的方法,或者更好的预编函数(比如apaches mime-magic)?谢谢你的帮助!

    4 回复  |  直到 15 年前
        1
  •  6
  •   Matchu    15 年前

    要仅解决此问题,可以执行以下操作:

    if(in_array($ext, $extensions))
    

    把这些扩展放到一个数组中。一定要检查文件的实际mimetype,而不是盲目地信任扩展名。

        2
  •  2
  •   tig Charlie Martin    15 年前

    用这种东西

    $extensions = array("bmp", "jpg", "png", "psd", "psp", "thm", "tif", "ai", "drw", "eps", "ps", "svg", "3dm", "dwg", "dxf", "pln");
    if (in_array($ext, $extensions))
    
        4
  •  -1
  •   ghostdog74    15 年前
       preg_match("/bmp|jpg|png|psd|psp|thm|tif|ai|drw|eps|ps|svg|3dm|dwg|dxf|pln/",$ext,$matches);
       print $matches[1];