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

17,991

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"
}
Share:
17,991
aCarella
Author by

aCarella

I work in logistics, and use databases as well as Office products to make efficient and effective applications for the operation that I am a part of. StackOverflow has really helped me so far, and I will continue to use it as a resource as well as contribute as much as I can to create a better understanding of what I know for other people. user:2664815

Updated on June 15, 2022

Comments

  • aCarella
    aCarella almost 2 years

    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 to application/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 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?

    • Anant - Alive to die
      Anant - Alive to die almost 9 years
      you don't have action in your form then how's it's possible. Or you are using jquery for that
    • Jonnix
      Jonnix almost 9 years
      Doesn't action default to the current URI if it's not set?
    • Admin
      Admin almost 9 years
      blank action = current url so you want ACTION="upload.php"
    • Anant - Alive to die
      Anant - Alive to die almost 9 years
      @John Stirling if you are asking then ok, but if you are telling then Can you see there are two files index.php and upload.php
    • Jonnix
      Jonnix almost 9 years
      @anantkumarsingh The upload.php could be included in index.php for all we know ;)
    • Anant - Alive to die
      Anant - Alive to die almost 9 years
      NO where include code is shown to me.thanks
    • aCarella
      aCarella almost 9 years
      I appreciate the comments. action not being set doesn't seem to be the issue; I've changed action to point to my php script, and this did not solve my problem.
    • Funk Forty Niner
      Funk Forty Niner almost 9 years
      you have many syntax errors and we're dealing with a file here, not a post array.
  • aCarella
    aCarella almost 9 years
    Thanks for the response. However, before doing the answer above, my file was being uploaded to my server but my $_POST variables were not being passed. After doing the answer above, my $_POST variables still won't pass and now my file will not upload onto my server either.
  • Jonathan
    Jonathan almost 9 years
    are you sure? because prior to the code change I made your code would have blown up as soon as it tried to move the uploaded file if no file was uploaded. what happens if you put var_dump($_FILES, $_POST); as the very first line in index.php
  • aCarella
    aCarella almost 9 years
    I get this: array(0) { } array(0) { }.
  • Jonathan
    Jonathan almost 9 years
    that's after your submit the form?
  • aCarella
    aCarella almost 9 years
    Just to be clear; again, in my original code, when I take out the enctype=multipart/form-data, then the POST variables work but the file will not upload. When I add enctype=multipart/form-data, my file uploads but my POST variables won't work.
  • Anant - Alive to die
    Anant - Alive to die almost 9 years
    @aCarella let me tell you that your query syntax is wrong and your code is working for me.
  • aCarella
    aCarella almost 9 years
    @anantkumarsingh Let me tell you that the query was edited so that none of my database information was posted online. That was not the focus of the question. Again, the script worked for uploading files when the encytpe was set but not for the $_POST variables, and when I got rid of the enctype the $_POST variables worked but the upload didn't.
  • plavozont
    plavozont over 8 years
    I had the same issue, found this topic, hoped it will help. It didn't. The problem have gone by itself while I was testing the example provided by @Augwa. All I did was changing input's type from hidden to text and reloading the page. Changing it back did not reproduce the issue :) Well I'm not sure I did only that, probably the problem was in some other part of my code(not related to the form itself), or in a browser's chache!
  • Parapluie
    Parapluie almost 7 years
    I fought for hours to figure out why my file upload wasn't working. Yours worked wonderfully when I tested it. It wasn't until I compared our code, line by line, that I realized I was only missing an "equals" sign. (name"name"). Yes. I had a little cry. Thank you.