$ _POST变量不能与$ _FILES和multipart/form-data一起使用

aCa*_*lla 8 php mysql forms post enctype

问题:每当我更改enctype我的HTML表单时multipart/form-data,$_POST变量不会填充在我的PHP脚本中.用户上传文件并填写表单,文件上传到服务器但$_POST变量不会填充.

码:

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

的index.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>
Run Code Online (Sandbox Code Playgroud)

我的PHP脚本试图更新我的MySQL数据库,以及在我的Ubuntu服务器上传我的文件如下.

upload.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();
Run Code Online (Sandbox Code Playgroud)

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

  • enctypeapplication/x-www-form-urlencoded.upload.php文件中都没有显示$_POST$_FILE数据.
  • application/x-www-form-urlencoded完全删除了.当我这样做时,我的$_POST变量工作,但我的文件没有上传.
  • 检查php.ini中post_max_size.在搜索互联网时,我遇到了一些关于类似问题的StackOverflow主题.根据我收集的内容,如果尝试上传的文件超过了值post_max_size,则$_POST变量将不会通过.我的php.ini文件中的值为post_max_size"8M",上传的测试文件图片为103 KiB.

如何获取$_POST数据以及从同一表单上传文件?

Aug*_*gwa 5

您只需要在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();
Run Code Online (Sandbox Code Playgroud)

尝试这段代码,看看它为你做了什么,如果这样做有效,另一方面没有,那就意味着我们需要解决问题的代码更多.

test.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>
Run Code Online (Sandbox Code Playgroud)

产量

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"
}
Run Code Online (Sandbox Code Playgroud)