【问题标题】:PHP and HTML Undefined Index on file upload文件上传时的 PHP 和 HTML 未定义索引
【发布时间】:2014-11-07 08:32:05
【问题描述】:

这是我的 HTML 代码

 <HTML>
<HEAD>
<TITLE>Upload a File</TITLE>
</HEAD>
<BODY>
<H1>Upload a File</H1>
<FORM METHOD="POST" ACTION="PHP3.php">
<strong>File to Upload:</strong><br>
<INPUT TYPE="file" NAME="txt1" SIZE="50">
<P><INPUT TYPE="submit" NAME="submit" VALUE="Upload File"></P>
</FORM>
</BODY>
</HTML>

这是我的 PHP 代码

if ($_FILES['txt1'] != '')
{
    mkdir("C:/xampp/CIS64/"); //Creates the CIS64 directory
    $filename = "C:/xampp/CIS64/"; //Location of where the file will be
    copy($_FILES['txt1']['tmp_name'], $filename.$_FILES['txt1']['name']) or die("Couldn't copy the file."); //Copies the uploaded file to the CIS64 directory
}
else
{
    die("No input file specified"); //If the file doesn't open, close the program.
}

由于某种原因,我收到错误消息:“未定义索引:第 11 行 C:\xampp\htdocs\PHP3.php 中的 txt1” 它以前工作过,突然停止工作。我的代码有什么问题?

【问题讨论】:

    标签: php html indexing undefined


    【解决方案1】:

    检查

    if (!empty($_FILES['txt1']))
    

    因为在没有发布表单的时候不会有像$_POST['txt1']这样的东西

    &lt;FORM METHOD="POST" ACTION="PHP3.php"&gt; 应该是 &lt;FORM METHOD="POST" ACTION="PHP3.php" enctype="multipart-formdata"&gt; 用于文件上传。

    【讨论】:

    • 这是我的问题!上传文件后,表格没有发布。我认为它在我的 HTML 代码中的某个地方,但我没有看到任何错误。
    • 好吧,它并没有解决它。但它显示了问题出在哪里。我不知道如何解决它。
    • 更新了我的答案。你错过了enctype 表单属性。
    【解决方案2】:
    1. 表单必须具有属性“enctype=multipart/form-data” 示例:&lt;form action=PHP3.php method=post enctype=multipart/form-data&gt;
    2. 在您的“PHP3.php”中添加字符串,例如var_dump($_POST);var_dump($_FILES);,然后检查里面的内容——帮助您调试!
    3. 我使用$location = $_SERVER['DOCUMENT_ROOT'] . '/myuploaddir/' . $filename;设置的保存位置

    【讨论】:

      猜你喜欢
      • 2020-03-08
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多