PHP上载图片因某些JPG文件而失败

时间:2018-10-27 13:25:07

标签: php html

我写了一张表格来上传图像并创建缩略图,它会检查以确保图像是JPG,PNG或GIF。在大多数情况下都可以使用,但是某些JPG文件被称为“无效文件格式”。

我尝试降低分辨率/文件大小,但是没有运气,但是如果将文件转换为.png,效果很好。

这里是指向JPG的链接,但失败; http://yourcomputerassistant.net/IMG0015.JPG

我的代码(为了使问题简短,我删除了缩略图的创建);

  if(isset($_FILES["userimage"]) && $_FILES["userimage"]["error"] == 0){

      $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");

      $userimage = $_FILES["userimage"]["name"];
      $filetype = $_FILES["userimage"]["type"];
      $targetDir = "uploads/";
      $targetFilePath = $targetDir . $userimage;
      $targetThumbFilePath = "uploads/thumbs/";

      if(!file_exists("uploads/" . $_FILES["userimage"]["name"])){


      // Verify file extension
        $ext = pathinfo($userimage, PATHINFO_EXTENSION);

        if(array_key_exists($ext, $allowed)) {

            if (move_uploaded_file($_FILES["userimage"]["tmp_name"], $targetFilePath)){
              $successmsg .= "<div class='alert alert-success'>Image - " . $userimage . " - Uploaded Successfully!</div>";

            }

        }   else {

              $error .= "Invalid File Format<br>";
            } 

        }   else { 

              $error .= "The Filename " . $_FILES["userimage"]["name"] . " already exists - please rename your picture before uploading<br>";
            }

    }

0 个答案:

没有答案