简体   繁体   中英

Image not being uploaded from HTML form or not being read through the PHP code

I am trying to add an image to from an HTML code into my MySQL database using PHP. However, as soon as I upload the image and try to process it, it gets lost in the transfer. HTML CODE:

<form class="" enctype="multipart/form-data" 
  action="complete-order.php?pid=<?php echo $row['pid']; ?>" method="post">
  <input type="submit" name="complete-submit" 
    value="Complete Order" style="float: right" />
  <div style="overflow: hidden; padding-right: .5em;">
    <input type="file" name="finalImg" required 
    placeholder="Final Image Order" style="width: 100%;" />
  </div>
  <label for="">Upload Final Image</label>
</form>

The PHP code is as follows. Currently, nothing is being echoed. I will add the image processing code inside the if condition but the $file variable is not getting anything.

// Save Final image
if(isset($_POST['complete-submit'])) {
    
    $file = $_FILES['finalImg'];
    if($file){
        echo "HERE";
    }
}

The global $_FILES will contain all the uploaded file information. Its contents from the example form is as follows. Note that this assumes the use of the file upload name userfile, as used in the example script above. This can be any name.

$_FILES["userfile"]["name"] The original name of the file on the client machine.

$_FILES["userfile"]["type"] The mime type of the file, if the browser provided this information. An example would be "image/gif". This mime type is however not checked on the PHP side and therefore don't take its value for granted.

$_FILES["userfile"]["size"] The size, in bytes, of the uploaded file.

$_FILES["userfile"]["tmp_name"] The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES["userfile"]["error"] The error code associated with this file upload.

You have two conditionals which need to evaluate to true before you would see any output, so start by troubleshooting those:

<?php

// troubleshoot
echo "<pre><tt>";
var_dump(isset($_POST['complete-submit']), $_FILES['finalImg']);
echo "</tt></pre>";

// Save Final image
if(isset($_POST['complete-submit'])) {
    
    $file = $_FILES['finalImg'];
    if($file){
        echo "HERE";
    }
}

Using your code and the built-in PHP 7.4 server, I see the following after submitting the form:

bool(true)
array(5) {
  ["name"]=>
  string(39) "Screenshot from 2020-12-02 15-15-54.png"
  ["type"]=>
  string(9) "image/png"
  ["tmp_name"]=>
  string(14) "/tmp/phphaJldU"
  ["error"]=>
  int(0)
  ["size"]=>
  int(238069)
}
HERE

So, it looks as though there's nothing wrong with your form or processing script. You may want to take a look at your webserver logs to see if there are any errors there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM