dongshan8194 2014-04-15 05:18
浏览 42

在php中为.JPG,.PNG Extensions上传文件

I read Image uploader, capital letters in JPG extension doesn't work but i need to upload .JPG files in my file uploading system.

Here is my Code :

<?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";
  }
?>

How can i change my code to make .JPG, .PNG files upload to my the upload directory ?

Error i am getting is invalid files if i choose .JPG files to upload.

Crazy try : I can't even upload if i change my code like this.

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

4条回答 默认 最新

  • drsb77336 2014-04-15 05:31
    关注

    if you mean you are having problems with uppercase extensions, then try converting the extension to lowercase in your conditions by using strtolower(), like:

    ...
    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
    
    评论

报告相同问题?