【问题标题】:Unable to upload JPEG file in the server无法在服务器中上传 JPEG 文件
【发布时间】:2013-07-23 10:12:26
【问题描述】:

我正在尝试使用 HTML 表单在我的 PHP 服务器中上传图像。我已经编写了以下代码。当我上传“png”文件时,它不会产生问题,但是当我上传“JPEG”图像时,它会显示“无效文件”。请检查并指导我。

谢谢

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="file_upload_test2.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



</body>
</html>


<?php

$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
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"))
&& ($_FILES["file"]["size"] < 200000)
&& 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"] / 200000) . " 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";
  }


?> 

【问题讨论】:

  • print $_FILES["file"]["type"], $_FILES["file"]["size"], $extension before if - 我想,你会自己解决你的问题那么
  • 如果我上传shell.php.png 会怎样?
  • 你的图片尺寸是多少.. php.ini 中的上传最大尺寸配置是多少,检查一下
  • 感谢您的回复。 php.ini 中的 max_size_configuration 存在问题。
  • @DamienPirsy 它说“文件无效”

标签: php html forms image-uploading image-upload


【解决方案1】:

如果您在 linux 上托管应用程序,则应将扩展名转换为小写以匹配 $allowedExts 中的字符串,因为 linux 区分大小写(而不仅仅是 linux)。

检查此How to track execution time of each line / block of lines / methods in PHP? 以便能够逐步运行脚本并查看问题出在哪里。

【讨论】:

    猜你喜欢
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2020-03-01
    • 2010-11-07
    • 2022-12-17
    • 1970-01-01
    相关资源
    最近更新 更多