9

问题:每当我将enctypeHTML 表单上的更改为 时multipart/form-data$_POST变量不会填充到我的 php 脚本中。用户在填写表格的同时上传文件,文件上传到服务器,但$_POST变量不填充。

代码:

我收集数据文本/图片的 HTML 表单。

索引.php

<form name="myForm" METHOD="POST" ACTION="" enctype="multipart/form-data">
  <input type="text" name="text1" id="text1">
  <input type="text" name="text2" id="text2">
  <input type="file" name="filebutton" id="filebutton">
  <input type="submit" name="submit" id="submit">
</form>

下面是我的 php 脚本,它尝试更新我的 MySQL 数据库,以及在我的 Ubuntu 服务器上上传我的文件。

上传.php

<?php
$uploaddir = "/var/www/img/pictures/";
$uploadfile = $uploaddir . basename($_FILES['filebutton']['name']);
move_uploaded_file($_FILES['filebutton']['tmp_name'], $uploadfile);
if (isset($_POST['filebutton'])) {  
    $pictureUpdate = ", PICTURE_FILEPATH = '" . $_POST['filebutton'] . "'";
} else {
    $pictureUpdate = "";
}
$connection = mysqli_connect("1.2.3.4","xxxx","xxxx","xxxx") or die("Caonnot connect to database.");
$update = "UPDATE table SET COLUMN1='" . $_POST['text1'] . "', COLUMN2='" . $_POST['text2'] . "' . $pictureUpdate . " where COLUMN3 = " . $_POST['text1'] . " ";
$update_sql = mysqli_query($connection, $update) or die("Cannot connect to mysql table. ". $update);
header("Location: " . $_SERVER['REQUEST_URL'] . "?success=1");
exit();

我试过的: 这是第一次这样做,所以我在这里有点自由造型,因为我似乎无法让它发挥作用。

  • 更改enctypeapplication/x-www-form-urlencoded. upload.php文件中没有出现$_POSTor$_FILE数据。
  • application/x-www-form-urlencoded彻底删除。当我这样做时,我的$_POST变量工作,但我的文件没有上传。
  • 检查php.inipost_max_size. 在搜索互联网时,我遇到了几个关于类似问题的 StackOverflow 主题。根据我收集的信息,如果尝试上传的文件超过 中的值post_max_size,则$_POST变量将不会通过。我的php.ini文件中的值为post_max_size“8M”,上传的测试文件图片为 103 KiB。

如何让$_POST数据工作,以及从同一个表单上传文件?

4

1 回答 1

6

您只需要在 if 语句中移动文件内容,然后更改$_POST['filebutton']$_FILES['filebutton']

每当您进行文件上传时,文件都会填充到$_FILES全局变量中,而其他字段会填充到$_POST全局变量中。

<?php
$uploaddir = "/var/www/img/pictures/";
if (isset($_FILES['filebutton'])) {  
    $uploadfile = $uploaddir . basename($_FILES['filebutton']['name']);
    move_uploaded_file($_FILES['filebutton']['tmp_name'], $uploadfile);
    $pictureUpdate = ", PICTURE_FILEPATH = '" . $_FILES['filebutton'] . "'";
} else {
    $pictureUpdate = "";
}
$connection = mysqli_connect("1.2.3.4","xxxx","xxxx","xxxx") or die("Caonnot connect to database.");
$update = "UPDATE table SET COLUMN1='" . $_POST['text1'] . "', COLUMN2='" . $_POST['text2'] . "' . $pictureUpdate . " where COLUMN3 = " . $_POST['text1'] . " ";
$update_sql = mysqli_query($connection, $update) or die("Cannot connect to mysql table. ". $update);
header("Location: " . $_SERVER['REQUEST_URL'] . "?success=1");
exit();

试试这个代码,看看它对你有什么作用,如果这个有效而另一个无效,那么这意味着你的代码还有更多我们需要解决问题。

测试.php

<form name="myForm" METHOD="POST" ACTION="" enctype="multipart/form-data">
  <input type="text" name="text1" id="text1">
  <input type="text" name="text2" id="text2">
  <input type="file" name="filebutton" id="filebutton">
  <input type="submit" name="submit" id="submit">
</form>
<xmp><?php if ($_SERVER['REQUEST_METHOD'] === 'POST') { var_dump($_FILES, $_POST); } ?></xmp>

输出

array(1) {
  ["filebutton"]=>
  array(5) {
    ["name"]=>
    string(21) "scanParser.properties"
    ["type"]=>
    string(24) "application/octet-stream"
    ["tmp_name"]=>
    string(14) "/tmp/phpRm1Ytp"
    ["error"]=>
    int(0)
    ["size"]=>
    int(264)
  }
}
array(3) {
  ["text1"]=>
  string(1) "1"
  ["text2"]=>
  string(1) "2"
  ["submit"]=>
  string(6) "Submit"
}
于 2015-06-04T21:21:58.023 回答