php - 在表单中上传多个 php 文件时,会将第一个文件名传递给其他上传

标签 php mysql forms file-upload

对,我有一个连接到 php 脚本的表单。表单中有多个输入,例如文本、复选框、下拉菜单等。但是,我还有 3 个上传槽,用于 3 张图像,这些图像上传到 img 文件夹,我只需获取名称即可与表单一起存储在 mysql 数据库中.

这是表格。

    <form method="post" action="scripts/AddProduct.php" enctype="multipart/form-data"><table class="table table-bordered table-striped">
        <tr>
            <td align="right">Category</td>
            <td><label><select id="pro_catagory" name"pro_catagory">
              <?php echo $result; ?>
              </select>
            </label></td>
          </tr>
          <tr>
            <td align="right">Subcatagory</td>
            <td><label>
              <select name="pro_subcategory" id="pro_subcategory">
              <option value="Hats">Hats</option>
              </select>
            </label></td>
          </tr>
          <tr>
            <td align="right">Details</td>
            <td><textarea name="pro_details" cols="" rows=""></textarea></td>
          </tr>
          <tr>
            <td align="right">Size</td>
            <td>
            S
            <input type="hidden" name="pro_size" value="0" /> 
            <input type="checkbox" name="pro_size" value="1" />
            M
            <input type="hidden" name="pro_size_m" value="0" /> 
            <input type="checkbox" name="pro_size_m" value="1" />
            L
            <input type="hidden" name="pro_size_l" value="0" /> 
            <input type="checkbox" name="pro_size_l" value="1" />
        </td>
          </tr>
          <tr>
            <td align="right">Colour</td>
            <td><label>
              <input type="text" name="pro_colour" id="colour"/>
            </label></td>
          </tr> 
          <tr>
            <td align="right">1st Image</td>
            <td><label>
              <input type="file" name="file" id="file" />
            </label></td>
          </tr>     

          <tr>
            <td align="right">2nd Image</td>
            <td><label>
              <input type="file" name="filee" id="filee" />
            </label></td>
          </tr> 
          <tr>
            <td align="right">3rd Image</td>
            <td><label>
              <input type="file" name="file3" id="file3" />
            </label></td>
          </tr>
<tr>
        <td>&nbsp;</td>
        <td><label>
          <input type="submit" name="button" id="button" value="Add This Item Now" />
        </label></td>
      </tr> </table></form>

(我已经缩短了脚本,因此它只是表单中内容的示例)。表格中共有 21 个选项。

接下来是 php 文件 AddProduct.php。

    <?php 
    //File Upload 1
    if ($_FILES["file"]["error"] > 0)
      {
      echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"] . "<br><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"]);
          }
        }
    //File Upload 2
    if ($_FILES["filee"]["error"] > 0)
      {
      echo "Error: " . $_FILES["filee"]["error"] . "<br>";
      }
    else
      {
    echo "Upload: " . $_FILES["filee"]["name"] . "<br>";
      echo "Type: " . $_FILES["filee"]["type"] . "<br>";
      echo "Size: " . ($_FILES["filee"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["filee"]["tmp_name"] . "<br><br>";

      if (file_exists("upload/" . $_FILES["filee"]["name"]))
          {
          echo $_FILES["filee"]["name"] . " already exists. " . "<br><br>";
          }
        else
          {
          move_uploaded_file($_FILES["filee"]["tmp_name"],
          "upload/" . $_FILES["filee"]["name"]);
          }
        }
    //File Upload 3
     if ($_FILES["file3"]["error"] > 0)
      {
      echo "Error: " . $_FILES["file3"]["error"] . "<br>";
      }
    else
      {
        echo "Upload: " . $_FILES["file3"]["name"] . "<br>";
      echo "Type: " . $_FILES["file3"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file3"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["file3"]["tmp_name"] . "<br><br>";

      if (file_exists("upload/" . $_FILES["file3"]["name"]))
          {
          echo $_FILES["file3"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file3"]["tmp_name"],
          "upload/" . $_FILES["file3"]["name"]);
          }
        }

  //Rest of php to get the posted values (all still work)
?>

我的问题是,当提交时,文件名(图像 1)会被跟踪到 filee 和 file3,因此输出结束如下:

Upload: testing.jpg
Type: image/jpeg
Size: 684.8818359375 kB
Stored in: C:\wamp\tmp\php41FF.tmp

testing.jpg already exists. Upload: 
Type: 
Size: 0 kB
Stored in: 

already exists. 

Upload: 
Type: 
Size: 0 kB
Stored in: 

感谢您提前提供的任何帮助

编辑:这是 var_dump

array (size=3)
  'file' => 
    array (size=5)
      'name' => string '2Fourk. RV2.jpg' (length=15)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp\tmp\php4FD7.tmp' (length=23)
      'error' => int 0
      'size' => int 701319
  'fileField2' => 
    array (size=5)
      'name' => string '2Fourk.jpg' (length=10)
      'type' => string 'image/jpeg' (length=10)
      'tmp_name' => string 'C:\wamp\tmp\php4FD8.tmp' (length=23)
      'error' => int 0
      'size' => int 938840
  'fileField3' => 
    array (size=5)
      'name' => string 'fiverrgigs.png' (length=14)
      'type' => string 'image/png' (length=9)
      'tmp_name' => string 'C:\wamp\tmp\php4FE9.tmp' (length=23)
      'error' => int 0
      'size' => int 18322 

最佳答案

我认为@drive235提出了一个很好的建议

我制作了这个简短的脚本。很肮脏。但也许它对您有帮助,因为您使用了很多行脚本。有时用更少的东西会更容易。

它遍历 $_FILES 数组并显示各种图像信息。

foreach($_FILES as $v){
          echo "<br />Upload: " . $v["name"]. "<br>";
          echo "Type: " .$v["type"] . "<br>";
          echo "Size: " . ($v["size"] / 1024) . " kB<br>";
          echo "Stored in: " . $v["tmp_name"] . "<br><br>";
    var_dump($v);
}

关于php - 在表单中上传多个 php 文件时,会将第一个文件名传递给其他上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20017933/

相关文章:

php - HTML 列表框使用 PHP 更新另一个列表框

javascript - 确保 MongoDB 中的点赞索引

php - 使用 WordPress REST API 通过 AJAX 更改密码

mysql - 结果包含多行错误 1172 mysql

php - 在数据库中记录成员生日的正确方法是什么?

javascript - 尝试访问 javascript 中的表单输入

php - 使用 PHP mysql_* SET NAMES UTF 8 和 Mysql Table With utf8_unicode_ci 进行字符串比较

mysql - docker-compose rails 与 mysql

python - Django:使用 forms.DecimalField 的 max_decimal_places 选项时出现 TypeError

C# 在 <form runat ="server"> </form> 中有多个客户端表单?