判断文件是否为图片的函数
PHP代码
- /**
- * Detect if a file is valid image file
- *
- * @param String $file
- * @return Boolean
- * @author Seaprince
- * @since 10/3/2008
- */
- function is_image_file($file) {
- // Pass 1
- if (!is_file($file)) return false;
- // Pass 2
- $allowed_ext = array(‘.jpg’,‘.gif’,‘.png’);
- $ext = strtolower(strrchr($file,‘.’));
- if (!in_array($ext, $allowed_ext)) return false;
- // Pass 3
- list($w,$h,$t,$a) = @getimagesize($file);
- return $w && $h && $t && $a;
- }
一个自我感觉不错的判断一个文件是否为合格图片的函数。
如果怕getimagesize会耗资源,可以不要这一个。