【问题标题】:PHP check if uploaded file is an imagePHP检查上传的文件是否是图像
【发布时间】:2017-06-11 12:39:57
【问题描述】:

我正在尝试创建用户头像。我只是想确保它的安全。检查文件是否是实际图像而不是其他任何东西的最佳方法是什么。

我试过了

$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";

    } else {
        echo "File is not an image.";
    }

虽然这似乎适用于某些图像,但其他图像(如照片)似乎使其失败。我用手机拍摄的照片似乎显示为“文件不是图像”,而其他照片则显示为图像。

我也一直在检查文件格式

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

}

【问题讨论】:

  • 你有$imageFileType=$_FILES["fileToUpload"]["type"]

标签: php image upload


【解决方案1】:

您可以直接使用getimagesize(),它将为非图像文件返回零值。

【讨论】:

  • 我上传某些照片时显示错误
  • 使用 $img = @imagecreatefromstring (@file_get_contents($path_to_img));如果结果为假,则您上传的文件不是图像。如果为真,请繁荣!
  • 很抱歉再次提到 getimagesize() 以及由于图像的元标记而面临的问题。 bcoz 大多数图像都包含隐藏的元标记,因此您的结果不准确,但 imagecreatefromstring 可以解决问题
  • "警告:此函数要求文件名是有效的图像文件。如果提供了非图像文件,可能会被错误地检测为图像,函数将成功返回,但数组可能“ - php.net/manual/en/function.getimagesize.php
【解决方案2】:

可以使用php5函数

mime_content_type (string $filename)

这里的 PHP 文档:http://php.net/manual/fr/function.mime-content-type.php

这将返回例如“image/jpg”。解析结果,瞧!这样您就不必遍历所有不同的类型。

【讨论】:

    【解决方案3】:

    <?php
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if(isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    }
    // Check if file already exists
    if (file_exists($target_file)) {
        echo "Sorry, file already exists.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_FILES["fileToUpload"]["size"] > 500000) {
        echo "Sorry, your file is too large.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
    && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
    ?>

    【讨论】:

    • 拒绝复制/粘贴 W3Schools 页面。这是大多数人发现此问题时试图排除故障的代码......
    猜你喜欢
    • 2013-03-02
    • 2016-10-06
    • 2014-11-25
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2014-03-11
    相关资源
    最近更新 更多