【问题标题】:php file upload with validation type带有验证类型的php文件上传
【发布时间】:2013-08-28 17:54:04
【问题描述】:

在php中我上传文件的代码是这样的

 $image_name= $_FILES['file']['name'];
  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);
  if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/jpg")
  || ($_FILES["file"]["type"] == "image/pjpeg")
  || ($_FILES["file"]["type"] == "image/x-png")
  || ($_FILES["file"]["type"] == "image/png"))
  && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
    else {
     move_uploaded_file($_FILES['file']['tmp_name'], $tmpName.$image_name);
    }
  }

现在,当我上传时,这段代码可以正常工作。但它不适用于文件类型验证。我用过 $allowedExts = array("gif", "jpeg", "jpg", "png"); to use only these types of file to upload. But this one is uploading any files type. So can someone kindly tell me where is the wrong part here. I want to upload only 仅限“gif”、“jpeg”、“jpg”、“png”文件。任何帮助和建议都会非常感激。谢谢。

【问题讨论】:

    标签: php html file-upload


    【解决方案1】:

    我记得我之前遇到过文件类型问题,但我无法解决,所以我决定继续做其他事情。 这是我检查上传的文件是否以前是“图像”的方法之一。我不认为检查文件是否真的是图像的好方法,但它确实对我有用,当时我想不出其他任何东西。

    <?php
        if(isAnImage($routetoyourfile)) {
            //Move the file
        } else {
            //Delete it
        }
        function isAnImage($fileroute) {
            $ext = pathinfo($fileroute, PATHINFO_EXTENSION);
            if($ext != "jpg" && $ext != "jpeg" && $ext != "png" && $ext != "JPG" && $ext != "PNG" && $ext != "JPEG") {
                return 0;
            } else {
                return 1;
            }
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-29
      • 2013-12-14
      • 2016-01-18
      • 2019-12-09
      • 2012-11-02
      • 2010-09-09
      • 1970-01-01
      • 2012-02-03
      相关资源
      最近更新 更多