问题:每当我将 HTML 表单上的 enctype
更改为 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>
我的 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();
我尝试过的: 这是第一次这样做,所以我在这里有点随意,因为我似乎无法让它发挥作用。
- 将
enctype
更改为application/x-www-form-urlencoded
。$_POST
或$_FILE
数据均未出现在 upload.php 文件中。 - 完全删除了
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
数据起作用,以及如何从同一表单上传文件?
最佳答案
你只需要将你的文件内容移动到你的 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();
试试这个代码,看看它能为您做什么,如果这个有效而另一个无效,那么这意味着您的代码还有更多我们需要解决的问题。
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>
output
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"
}
关于php - $_POST 变量不适用于 $_FILES 和 multipart/form-data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30654310/