在php中为上传文件.JPG.PNG扩展


File upload in php for .JPG, .PNG Extensions

我读过图像上传器,JPG扩展中的大写字母没有';不起作用,但我需要上传。JPG文件在我的文件上传系统中。

这是我的代码:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png" , "JPG");
$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/JPG")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

如何更改我的代码以使。JPG。PNG文件是否上传到我的上传目录?

如果我选择,我得到的错误是无效文件。要上传的JPG文件。

疯狂的尝试:如果我这样更改代码,我甚至无法上传。

$allowedExts = array("gif", "jpeg", "jpg", "png" , "JPG");
|| ($_FILES["file"]["type"] == "image/JPG")

在这里,这将帮助您(我希望)

http://www.codeproject.com/Tips/660367/PHP-image-uploader-with-display-insert

http://www.htmlgoodies.com/beyond/php/article.php/3877766

如果你的意思是大写扩展有问题,那么试着在你的条件下使用strtolower()将扩展转换为小写,比如:

...
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/JPG")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array(strtolower($extension), $allowedExts))
{
...//rest of your code
    $temp = $_FILES["file"]["name"];    
    $valid_formats = array("jpg", "png", "gif", "bmp", "JPG", "PNG", "GIF", "BMP");
    list($txt, $ext) = explode(".", $temp);
    if(in_array($ext,$valid_formats)){
     if ($_FILES["file"]["error"] > 0){
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
     }
     else{
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
}
else{
  echo "Invalid file";
}

试试这个代码,它可能会帮助你。。。。

if(file_exists($path_image) && in_array(exif_imagetype($path_image),array(1, 2, 3, 6))){ ... }

验证文件存在并且类型为图像

http://php.net/manual/en/function.exif-imagetype.php