windows下面如不能正常工作: 可参考:http://www.php.net/manual/en/fileinfo.installation.php#82570 下载file-5.03-bin.zip ,解压出来,在其中的share目录有magic.mgc 、magic 两个文件。 然后添加一个名为MAGIC的系统环境变量指向magic 文件。如D:softwarePHPextrasmiscmagic 复制代码 代码如下: function getFileMimeType($file) { $buffer = file_get_contents($file); $finfo = new finfo(FILEINFO_MIME_TYPE); return $finfo->buffer($buffer); } $mime_type = getFileMimeType($file); switch($mime_type) { case "image/jpeg": // your actions go here... }
处理图像上传 如果你打算只允许图像上传,那么你可以使用内置的getimagesize()函数,以确保用户实际上是上传一个有效的图像文件。如果该文件不是有效的图像文件,这个函数返回false。 复制代码 代码如下: // 假设file input 域的name 属性为myfile $tempFile = $_FILES["myFile"]["tmp_name"]; // path of the temp file created by PHP during upload $imginfo_array = getimagesize($tempFile); // returns a false if not a valid image file if ($imginfo_array !== false) { $mime_type = $imginfo_array["mime"]; switch($mime_type) { case "image/jpeg": // your actions go here... } } else { echo "This is not a valid image file"; }