【问题标题】:Undefined Index when using $_FILES to upload a picture [duplicate]使用 $_FILES 上传图片时的未定义索引 [重复]
【发布时间】:2016-06-18 15:20:01
【问题描述】:

我正在使用PHP 创建一个应用程序,我根本不是这方面的专家,但我正在慢慢完成它。

我在允许用户在图像旁边上传信息时遇到了一个问题。

下面是我设置的表单,允许用户输入所有信息并选择他们希望上传的图像:

<form action="NewOwnedItem.php" method="POST">
      <div class="input-row">
        <label>Item's Name</label>
        <input type="text" name="Name" placeholder="Enter your item's name">
      </div>
      <div class="input-row">
        <label>Image</label>
        <input type="file" name="file_img">
      </div>
      <div class="input-row">
        <label>Description</label>
        <input type="text" name="Description" placeholder="Enter your item's description">
      </div>
      <div class="input-row">
        <label>Quantity</label>
        <input type="number" name="Quantity" min="1" max="99"  placeholder="Enter the quantity you own of this item">
      </div>
      <div class="input-row">
        <label>Price</label>
        <input type="text" name="Price" placeholder="Enter the price you paid for this item">
      </div>
      <div class="input-row">
        <label>Condition</label>
        <select name="Condition">
          <option value="Very Poor">Very Poor</option>
          <option value="Poor">Poor</option>
          <option value="Fair">Fair</option>
          <option value="Good">Good</option>
          <option value="Very Good">Very Good</option>
          <option value="Excellent">Excellent</option>
          <option value="Near Mint">Near Mint</option>
          <option value="Mint">Mint</option>
        </select>
      </div>
      <div class="input-row">
        <label>Date Purchased</label>
        <input type="date" name="DatePurchased">
      </div>

      <input type="submit" value="Add Item" name="submit">

    </form>

以下是获取此信息并将其存储在数据库中的 PHP 代码(与数据库的连接在文件的前面,因此此处未显示)。 我想要它,以便图像 URL 存储在数据库中,而实际图像存储在服务器上的“上传”文件中,我遵循了该部分代码的教程,但错误仍然出现。

注意:未定义索引:file_img in /web/users/n3071633/FYP/NewOwnedItem.php 第 50 行 注意:未定义 索引:file_img in /web/users/n3071633/FYP/NewOwnedItem.php 上线 51

这些是出现的错误,下面是 PHP 代码。

<?php

    if(isset($_POST['submit'])){

      $Name = mysql_escape_string($_POST['Name']);
      $Description = mysql_escape_string($_POST['Description']);
      $Quantity = mysql_escape_string($_POST['Quantity']);
      $Price = mysql_escape_string($_POST['Price']);
      $Condition = mysql_escape_string($_POST['Condition']);
      $DatePurchased = mysql_escape_string($_POST['DatePurchased']);

      $filetmp = $_FILES["file_img"]["tmp_name"];
      $filename = $_FILES["file_img"]["name"];
      $filepath = "uploads/".$filename;

      move_uploaded_file($filetmp,$filepath);

      mysql_query("INSERT INTO `CS_Items` (`Item_ID`, `Name`, `Image`, `Description`, `Quantity`, `Price`, `ItemCondition`, `DatePurchased`, `User_ID`, `ItemStatus`) VALUES (Null, '$Name', '$filepath', '$Description', '$Quantity', '$Price', '$Condition', '$DatePurchased', '$login_session', 'Owned') ");
    }
  ?>

让我感到困惑的是,表单的所有其他部分都有效,但包含 $_FILES 的部分似乎是那些显然没有定义的部分?

我尝试查看其他问题和答案,但答案似乎是我的代码中已经存在的东西?任何帮助将不胜感激

【问题讨论】:

  • 你忘了在你的&lt;form action="NewOwnedItem.php" method="POST"&gt; 中添加enctype = "multipart/form-data" 所以没有它你永远不会得到$_FILES
  • @Anant 你应该回答并接受它。

标签: php file undefined-index


【解决方案1】:

您忘记在您的&lt;form action="NewOwnedItem.php" method="POST"&gt; 中添加enctype="multipart/form-data",所以如果没有,您将永远无法使用$_FILES

所以让它像:-

&lt;form action="NewOwnedItem.php" method="POST" enctype="multipart/form-data"&gt;

【讨论】:

  • 感谢 Anant 让错误消失并在数据库中上传文件路径!
猜你喜欢
  • 1970-01-01
  • 2014-04-29
  • 2019-07-04
  • 2017-01-06
  • 2014-02-11
  • 2018-08-28
  • 1970-01-01
  • 2017-12-01
  • 1970-01-01
相关资源
最近更新 更多