0

The problem

The $_FILES['file'] array is set, yet it's empty whenever I try to use it.

What I tried

  • Googling
  • Setting file upload to On in php.ini (both Xampp and project file root)
  • Uploading one file at a time (just a wild try at fixing the problem
  • Debugging the entire code for a month trying to solve this problem

What I know for a fact

  • The path to the onSubmit is correct
  • The name of the input in the form and the name after $_FILES['file'] is exactly the same
  • The form has all it's required attributes
  • The input has type="file" and multiple in it

My code for the form(HTML) and the file engine(PHP)

<html>
<form method="POST" action="../php/post.php" enctype="multipart/form-data">
<h3>Title</h3>
<input type="hidden" name="case" value=1>
<input type="title" name="pname">
<h3>Message</h3>
<input type="message" name="pmsg">
<h3>Images</h3>
<input type="file" name="pimg[]" multiple>
<input class="submit" type="submit" value="Upload">
</form>
</html>

PHP

<?php
if (!empty($_FILES['file']['pimg'])){
$noFiles = 1;
echo "Files found...\n";
} else {
$noFiles = 0;
echo "Files not found...\n";
echo (!empty($_FILES['file']['pimg']));
echo $_FILES['file']['pimg'][0];
}
?>

Output

The If determines the array is empty, the last echo causes an error

Neon Foxer
  • 17
  • 6
  • 2
    what is `$_FILES['file']['pimg']`? Isn't it `$_FILES['pimg']`? – u_mulder May 27 '22 at 14:23
  • 2
    Try to be *specific* in your description of what happens. You say "the last echo causes an error", but don't show us what the error says. Even if you don't know what it means, showing it to us will allow us to explain it to you. You could also add the output of `var_dump($_FILES);` which will probably reveal the problem. – IMSoP May 27 '22 at 14:23
  • And all this can be solved with __one line of code__: `print_r($_FILES);` – u_mulder May 27 '22 at 14:24
  • I have tried print_r(); it returns nothing. The error just says that ['pimg'] doesnt exist – Neon Foxer May 27 '22 at 14:27
  • 2
    _"I have tried print_r(); it returns nothing."_ `print_r($_FILES);` will always show something. – shingo May 27 '22 at 14:36
  • This makes no sense. Put `var_dump($_FILES);` as the first line in post.php and paste the exact, full response here. Also please stop abbreviating or paraphrasing the error message, its really not helpful. It's like telling the doctor only half your symptoms! Paste the exact, full error message and tell us which line caused it – ADyson May 27 '22 at 14:36
  • Also, your code never checks for potential errors in the upload...Read about it here: https://www.php.net/manual/en/features.file-upload.post-method.php and here: https://www.php.net/manual/en/features.file-upload.errors.php – ADyson May 27 '22 at 14:40
  • To check not empty use $_FILES['pimg']['name'] use like this – Vicky May 27 '22 at 15:00
  • _The error just says that ['pimg'] doesnt exist_ that's your clue. – AbraCadaver May 27 '22 at 15:14

1 Answers1

-1

You're not far off. $_FILES['file']['pimg'] should be $_FILES['pimg'] You should also check to see if there is something selected. $_FILES['pimg']['size'] checks the filesize. If 0, nothing is selected

This way works.

<?php

    if ($_FILES['pimg']['size'] != 0){
        $noFiles = 1;
        echo "Files found...\n";
        echo $_FILES['pimg']['name'][0];
    } else {
        $noFiles = 0;
        echo "Files not found...\n";
    
    /* This will always be empty placed here as no files have been found */
    //echo (!empty($_FILES['file']['pimg']));
    //echo $_FILES['file']['pimg']['name'][0];
    }

?>
SourceCode
  • 16
  • 2