【问题标题】:unable to upload and move file with php无法使用php上传和移动文件
【发布时间】:2013-06-21 11:19:02
【问题描述】:

代码如下:

<html>
<body>

<form action="upload_file.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"] < 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";
  }
?>

我在尝试处理 .png 或任何其他图像文件时收到错误“无效文件”。

【问题讨论】:

  • 您上传的文件有多大?该脚本会将其限制为小于 20k。
  • 这个“upload_file.php”是包含表单的文件名吗?
  • 您在说“无效文件”之前检查的众多条件中的哪一个失败了?
  • 不要使用此代码。您允许恶意用户在您的服务器上随意书写他们选择的文件。至少文件名将被限制为图像类型的文件扩展名,但仍然......这段代码很糟糕。不要使用 w3fools 教程代码。充其量他们的例子是垃圾。他们最糟糕的情况会被污水厂拒绝。

标签: php forms file-upload photo


【解决方案1】:

这是一个出色的图像上传脚本的链接,该脚本能够即时进行数百种可能的图像修改 class.upload.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    相关资源
    最近更新 更多