检查图像的输入文件类型

我正在尝试检查在我的多个输入字段中,图像是否真的是图像。问题是,我的代码仍然允许上传 pdf 和其他文件。我在这里阅读了有关它的问题,但这不是我发现的唯一东西,但我真的不明白我应该如何使用它。这是它的样子:


$selectid = $conn->prepare("SELECT max(id) AS id FROM cards");

$selectid->execute();

$cardid = $selectid->get_result()->fetch_array()['id'];


$stmt = $conn->prepare("INSERT INTO cardimages(image, cardid) VALUES(?, ?)");


$target_file = $_FILES['file']['tmp_name'];

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));


if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}

if (count($_FILES['file']['tmp_name']) > 5) {

    header("Location: addbusiness.php?message=3");

} else {

    $everyPicUnder2Mb = true;

    for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {

        if ($_FILES["file"]["size"][$i] > 2100000) {

            $everyPicUnder2Mb = false;

        }

    }



    if ($everyPicUnder2Mb) {

        for ($i = 0; $i < count($_FILES['file']['tmp_name']); $i++) {

            $file = $_FILES['file']['tmp_name'][$i];

            if (is_uploaded_file($file)) {

                $imgData = file_get_contents($file);

                $stmt->bind_param("si", $imgData, $cardid);

                $stmt->execute();

            }

        }

    } else {

        header("Location: addbusiness.php?message=2");

    }

}


慕的地8271018
浏览 138回答 1
1回答

桃花长相依

我是这样做的:$allowed_types = array (IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);$detectedType = exif_imagetype($_FILES['file']['tmp_name'][$i]);if (!in_array($detectedType, $allowed_types))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo 'Only JPG/JPEG/PNG/GIF';&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP