【问题标题】:Undefined Index when uploading file [duplicate]上传文件时未定义的索引[重复]
【发布时间】:2018-02-27 17:46:34
【问题描述】:

我有以下代码
index.html

<form action="upload.php" method="POST" enctype="multipart/form-data">
<input type="file" name="test" id="test" class="input"><br>
<input type="submit" value="Go!" class="input btn">
</form>

上传.php

<?php
$x = $_FILES["test"]["name"];
echo $x;
?>

它会返回这个

注意:未定义索引:在第 2 行的 C:\xampp\htdocs\example\upload\upload.php 中测试

我不知道这里发生了什么,我觉得一切正常吗?

我见过PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset",但这对我没有帮助

【问题讨论】:

  • 它抱怨索引“testin”,但您发布的代码中不存在该索引。
  • 我的错,已编辑。只是一个错字
  • 仅供参考,您应该始终按原样复制/粘贴代码、错误等,而不是手动抄录。
  • 要排除故障,您可以尝试输出$_FILES 数组。类似echo "&lt;pre&gt;".print_r($_FILES,true)."&lt;/pre&gt;";
  • 顺便说一句;你不能只这样做$x = $_FILES["test"]["name"]; - 因为首先需要一个 tmp 名称。 PHP 还不知道它是什么名字,因为它从来没有被上传过。

标签: php html forms file-upload


【解决方案1】:

您应该验证您的 php.ini 文件,当您尝试上传的文件超出允许的大小时,服务器会抱怨并且不会填充 $_FILES 变量。

【讨论】:

    猜你喜欢
    • 2013-10-02
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多