【问题标题】:Notice: Undefined index: file [duplicate]注意:未定义的索引:文件[重复]
【发布时间】:2017-04-08 14:01:55
【问题描述】:

已经回答了许多类似的问题(我看到与此相关的每个问题都被标记为重复)但我找不到任何可以解决这个问题的方法。我不擅长 PHP,我才刚刚开始。无论哪种方式,我都会收到“注意:未定义的索引:第 12 行的 Applications/XAMPP/xamppfiles/htdocs/upload_file.php 中的文件”。

我从这里的其他人那里得到了这个代码,他以前遇到过问题。 cmets 中的某个人修复了他的错误,所以我复制了他的代码,然后做了同样的事情。但是现在我的代码不起作用,我该怎么办?

代码

  <?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
//removed comment
//removed comment
$allowedExts = array("mp4", "mpg", "mov", "flv", "avi");
$temp = explode(".", $_FILES["file"]["name"]);
print_r($_FILES["file"]["type"]);
$extension = end($temp);
if (($_FILES["file"]["size"] < 200000)) {
  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("uploads/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "uploads/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>

编辑:

我被要求提供 html 表单,所以你去吧。

<!DOCTYPE html>
<html>
<body>

<form action="upload_file.php" method="post" enctype="multipart/form-    data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Video" name="submit">    
</form>

</body>
</html>

【问题讨论】:

  • 你能贴出连接到这个的html表单吗
  • 确定现在就做
  • 一般评论:通过查看文件类型的“文件扩展名”来测试文件类型是一种非常没用的方法。 人敢于重命名文件,你知道吗?
  • 您没有输入名称为file。结束了。
  • 基本上,您的输入被命名为fileToUpload,而不是file...

标签: php file upload


【解决方案1】:

我觉得

<input type="file" name="fileToUpload" id="fileToUpload"> 

应该是

<input type="file" name="file" id="fileToUpload">

【讨论】:

  • 谢谢!不知道谁对此投了反对票,但它为我解决了这个问题。我收到另一个错误,但我可以修复那个错误。 :D
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-28
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 2017-08-26
相关资源
最近更新 更多