代码如下
/** * 获取文件后缀名,并判断是否合法 * * @param string $file_name * @param array $allow_type * @return blob*/function get_file_suffix($file_name, $allow_type = array()){ $fnarray=explode('.', $file_name); $file_suffix = strtolower(array_pop($fnarray)); if (empty($allow_type)) { return $file_suffix; } else { if (in_array($file_suffix, $allow_type)) { return true; } else { return false; } }}
测试
$allow_wj="jpg,gif,png,jpeg";$allow=explode(",",$allow_wj); if (get_file_suffix("sakjdfk1.jpg",$allow)){ echo "ok";}else{ echo "no";}
结果
ok
更多php相关知识,请访问!
以上就是php 判断上传的文件是否合法的详细内容。