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.
enctype
to application/x-www-form-urlencoded
. Neither the $_POST
or $_FILE
data showed up in the upload.php file.application/x-www-form-urlencoded
altogether. When I do this, my $_POST
variables work, but my file does not upload.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 in post_max_size
, then $_POST
variables will not go through. The value in my php.ini file for post_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?
Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.
Create The HTML Form Make sure that the form uses method="post" The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form.
Multipart upload allows you to upload a single object as a set of parts. Each part is a contiguous portion of the object's data. You can upload these object parts independently and in any order. If transmission of any part fails, you can retransmit that part without affecting other parts.
The default content type/enctype is “application/x-www-form-urlencoded”. This is not efficient for sending large quantities of binary data or text containing non-ASCII characters. Multipart/form-data should be used for submitting forms that contain large files, non-ASCII data, and large binary data.
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With