[英]$_POST variables not working with $_FILES and multipart/form-data
问题:每当我将HTML表单上的enctype
更改为multipart/form-data
, $_POST
变量都不会填充在我的php脚本中。 用户上传文件并填写表单,文件上传到服务器,但不会填充$_POST
变量。
码:
我的HTML表单,用于收集数据文本/图片。
<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
$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
。 file. $_POST
或$_FILE
数据均未显示在文件中。 application/x-www-form-urlencoded
。 当我这样做时,我的$_POST
变量可以工作,但是我的文件没有上传。 post_max_size
. 检查中的post_max_size
。 在互联网上搜索时,我遇到了一些关于类似问题的StackOverflow主题。 根据我的收集,如果尝试上传的文件超过了post_max_size
的值,那么$_POST
变量将不会通过。 file for post_max_size
says "8M", and the test file picture being uploaded is 103 KiB. 我的文件中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>
输出
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"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.