且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP 上传文件 - 仅图像检查

更新时间:2023-11-25 17:56:46

是的,很容易.但首先,您需要一些额外的位:

Yes, quite easily. But first off, you need some extra bits:

// never assume the upload succeeded
if ($_FILES['file']['error'] !== UPLOAD_ERR_OK) {
   die("Upload failed with error code " . $_FILES['file']['error']);
}

$info = getimagesize($_FILES['file']['tmp_name']);
if ($info === FALSE) {
   die("Unable to determine image type of uploaded file");
}

if (($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
   die("Not a gif/jpeg/png");
}

相关文档:文件上传错误getimagesize图像常量.