$ _POST variables do not work with $ _FILES and multipart / form-data

Problem: Whenever I change the enctype in my HTML form to multipart/form-data , the $_POST variables are not populated in my PHP script. Users upload files along with filling out the form, and files are uploaded to the server, but the $_POST variables are not populated.

Code:

My HTML form that collects text / image data.

<i> 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 is trying to update my MySQL database as well as upload my file on my Ubuntu server below.

<i> 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 tried: This is the first time I do this, so I am a little freestyling here because I can not get this to work.

  • Changed enctype to application/x-www-form-urlencoded . $_POST or $_FILE did not appear in the upload.php file.
  • Removed application/x-www-form-urlencoded as a whole. When I do this, the $_POST variables change, but my file does not load.
  • Checked by php.ini for post_max_size . After searching the Internet, I came across several StackOverflow topics regarding similar issues. From what I compiled, if the file trying to be uploaded exceeds the value in post_max_size , then the $_POST variables will fail. The value in my php.ini file for post_max_size says "8M" and the uploaded image of the test file is 103 KiB.

How do I get $_POST data to work, as well as upload a file from the same form?

+6
source share
1 answer

You just need to move the file to your if statement and change $_POST['filebutton'] to $_FILES['filebutton']

Whenever you upload a file, the files are filled in the global variable $_FILES , and the rest of the fields are filled in the global variable $_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(); 

try this code and see what it does for you, if it works, and the other doesn’t, it means that for 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" } 
+5
source

Source: https://habr.com/ru/post/988536/


All Articles