$ _POST变量不能与$ _FILES和multipart / form-data一起使用 [英] $_POST variables not working with $_FILES and multipart/form-data
问题描述
enctype
更改为 multipart / form-data
, $ _ POST
变量不会填充到我的php脚本中。用户上传文件并填写表单,并将文件上传到服务器,但 $ _ POST
变量不会填充。 代码:
收集数据文本/图片的HTML表单。
index.php
< form name =myFormMETHOD =POST ACTION =enctype =multipart / form-data>
< input type =textname =text1id =text1>
< input type =textname =text2id =text2>
< input type =filename =filebuttonid =filebutton>
< input type =submitname =submitid =submit>
< / form>
我的php脚本试图更新我的MySQL数据库,并且在我的Ubuntu服务器上上传我的文件
upload.php
<?php
$ uploaddir =/ var / www / img / pictures /;
$ uploadfile = $ uploaddir。基名($ _ FILES [ filebutton] [名称]);
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)或die(Caonnot connect to database。);
$ update =UPDATE table SET COLUMN1 ='。 $ _POST ['text1']。 ',COLUMN2 ='。 $ _POST ['text2']。 '。$ pictureUpdate。其中COLUMN3 =。$ _POST ['text1']。;
$ update_sql = mysqli_query($ connection,$ update)或死(无法连接到mysql表。)。
header(Location:。$ _SERVER ['REQUEST_URL']。?success = 1);
exit();
我试过的东西:
这是第一次这样做,所以我在这里挺自由的因为我似乎无法得到此工作。
- 将
enctype
更改为application / x-www-form-urlencoded
。$ _ POST
或$ _ FILE code>数据显示在 upload.php 文件中。
- 已删除
application / x-www-form-urlencoded
当我这样做时,我的$ _ POST
变量有效,但是我的文件没有上传。 检查 php.ini 为post_max_size
在搜索互联网时,我遇到了一些有关类似问题的StackOverflow主题。 t我收集到了,如果试图上传的文件超过了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。基名($ _ FILES [ filebutton] [名称]);
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)或die(Caonnot connect to database。);
$ update =UPDATE table SET COLUMN1 ='。 $ _POST ['text1']。 ',COLUMN2 ='。 $ _POST ['text2']。 '。$ pictureUpdate。其中COLUMN3 =。$ _POST ['text1']。;
$ update_sql = mysqli_query($ connection,$ update)或死(无法连接到mysql表。)。
header(Location:。$ _SERVER ['REQUEST_URL']。?success = 1);
exit();
试试这个代码,看看它对你有什么作用,如果这样做,而另一个不然,那意味着你需要解决更多的代码
test.php
< form name =myFormMETHOD =POSTACTION =enctype =multipart / form-data>
< input type =text name =text1id =text1>
< input type =textname =text2id =text2>
< filebuttonid =filebutton>
< input type =submitname =submitid =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] =>
字符串(1)1
[text2] =>
string(1)2
[submit] =>
string(6)Submit
}
Problem: Whenever I change the enctype
on my HTML form to multipart/form-data
, $_POST
variables do not populate in my php script. Users upload files along with filling out a form, and the files upload to the server but the $_POST
variables do not populate.
Code:
My HTML form that collects the data text/picture.
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>
My php script that attempts to update my MySQL database, as well as upload my file on my Ubuntu server is below.
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();
What I've Tried: This is the first time doing this, so I'm kinda freestyling here because I cannot seem to get this to work.
- Changed the
enctype
toapplication/x-www-form-urlencoded
. Neither the$_POST
or$_FILE
data showed up in the upload.php file. - Removed
application/x-www-form-urlencoded
altogether. When I do this, my$_POST
variables work, but my file does not upload. - Checked php.ini for
post_max_size
. Upon searching the internet, I've come across a couple StackOverflow topics concerning similar issues. From what I've gathered, if the file trying to be uploaded exceeds the value inpost_max_size
, then$_POST
variables will not go through. The value in my php.ini file forpost_max_size
says "8M", and the test file picture being uploaded is 103 KiB.
How do I get $_POST
data to work, as well as uploading a file from the same form?
You just need to move your file stuff inside your if statement, and change $_POST['filebutton']
to $_FILES['filebutton']
Whenever you do a file upload, the files get populated in the $_FILES
global variable, and the other fields get populated in the $_POST
global variable.
<?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();
try this code, and see what it does for you, if this works and the other does not then that means there's more to your code we need to solve the problem.
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"
}
这篇关于$ _POST变量不能与$ _FILES和multipart / form-data一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!