PHP upload image -> Invalid file

自作多情 提交于 2019-12-12 03:46:45

问题


i'm on archlinux with lampp(last version) i learn php from w3c-school, i'm on page upload file and here my script can't upload nothing the only thing return me is Invalid File ever (.jpg .png ecc..) here is the code:

 <?php


include "config_db.php";

$name = "/upload/" . $_FILES['file']['name'];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000)
&& 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"];
      echo '<img src="upload/' . $_FILES["file"]["name"] . '">';
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

回答1:


Your are checking extension in incorrect way. Here is how it should be:

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
header ("Content-Type: text/plain");
if ((($_FILES["file"]["type"] == "image/gif")
  || ($_FILES["file"]["type"] == "image/jpeg")
  || ($_FILES["file"]["type"] == "image/png")
  || ($_FILES["file"]["type"] == "image/pjpeg"))
  && ($_FILES["file"]["size"] < 100000)
  && in_array ($extension, $allowedExts)) echo "Success";
else echo "Error";

This code works fine for me together with the following form:

<form method="POST" enctype="multipart/form-data" action="upload.php">
  <input type="file" name="file" />
  <input type="submit" />
</form>



回答2:


If image name contains .(dot) then there is a problem. For example if the image name is like 12.212.jpg, when you explode the image name with .(dot) your extension will be .212. so in this case you need to use like below code.

$name = "/upload/" . $_FILES['file']['name'];
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = explode(".", $_FILES["file"]["name"]);
$extension = end($extension);

It will give you the exact image format.




回答3:


Your error is in below line.

$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);

Must be as below.

$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

And also change if condition as below.

if (in_array($extension, $allowedExts))



回答4:


change this condition

&& in_array($extension, $allowedExts))

to only this condition

&& $extension)

because you used in_array 2 times, 2nd one is unnecessary.




回答5:


I think the problem is in in_array() function. You have already checked the extension in this line,

$extension = in_array(explode(".", $_FILES["file"]["name"]), $allowedExts);

Now $extension is value is TRUE.

So you cannot check this $extension in the if condition. Instead of that,

just check whether $extension is TRUE,

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 100000)
&& $extension)



回答6:


I ran into the problem where if the extension was caps (i.e. image.JPG) it gave me invalid file. I used this code:

$t_image = strtolower($_FILES['file']['name']);

to solve the problem.



来源:https://stackoverflow.com/questions/15127418/php-upload-image-invalid-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!