问题:每当我将enctype
HTML 表单上的更改为 时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();
我试过的: 这是第一次这样做,所以我在这里有点自由造型,因为我似乎无法让它发挥作用。
- 更改
enctype
为application/x-www-form-urlencoded
. upload.php文件中没有出现$_POST
or$_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
数据工作,以及从同一个表单上传文件?