1

I've created a form for multi-file upload. I report here only the required code, of it.

when I upload one or more files, the array

$_FILES['shopImage']

has the date related the one or more files, and I can see it through the var_dump() I put into the PHP code here below I can spot the uploaded file.

however, the function

is_uploaded_file($_FILES['shopImage']['tmp_name'])

returns always FALSE.

Please here the HTML code that generates the $_FILES array:

<form method="POST" action="upload.php" enctype="multipart/form-data">
...
...
...
    <div class="pics">
        <input type="file" name="shopImage[]">
        <input type="file" name="shopImage[]">
        <input type="file" name="shopImage[]">
        <input type="file" name="shopImage[]">
    </div>
...
...
...
    <input type="submit" name="submit" value="UPDATE"><input type="reset" value="RESET">
</form>

Here the PHP code I used to test it:

<?
require_once $_SERVER['DOCUMENT_ROOT'].'include/setup.php';

if (!empty($_POST['submit']) and $_POST['submit'] == "UPDATE") {

    var_dump($_FILES['shopImage']);
    if(is_uploaded_file($_FILES['shopImage']['tmp_name'])){
        echo "TRUE";
        exit;
    } else {
        echo "FALSE";
        exit;
    }
}
?>

And here here the var_dump()

array(5) {
  ["name"]=>
  array(10) {
    [0]=>
    string(11) "Logo.png"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
  }
  ["type"]=>
  array(10) {
    [0]=>
    string(9) "image/png"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
  }
  ["tmp_name"]=>
  array(10) {
    [0]=>
    string(14) "/tmp/php2TOmvR"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
    [3]=>
    string(0) ""
  }
  ["error"]=>
  array(10) {
    [0]=>
    int(0)
    [1]=>
    int(4)
    [2]=>
    int(4)
    [3]=>
    int(4)
  }
  ["size"]=>
  array(10) {
    [0]=>
    int(21947)
    [1]=>
    int(0)
    [2]=>
    int(0)
    [3]=>
    int(0)
  }
}

NOTE: I did check a quite similar issue here, but the conditions described, are not the same of mine. I didn't implement yet move_upload_file() etc ...

Could you help me please to figure out a solution?

Thanks a lot in advance

Tormy Van Cool
  • 658
  • 10
  • 29
  • Do you __see__ that `$_FILES['shopImage']['tmp_name']` is __array__? – u_mulder Apr 11 '18 at 19:26
  • 1
    You have multiple files, which one? `$_FILES['shopImage']['tmp_name'][0]` – AbraCadaver Apr 11 '18 at 19:27
  • 1
    Possible duplicate of [Multiple file upload in php](https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php) – IncredibleHat Apr 11 '18 at 19:31
  • @IncredibleHat it's NOT a duplicate ... please ...I read the thread you refer to, and no solutions for my case – Tormy Van Cool Apr 11 '18 at 19:32
  • @AbraCadaver the goal is to upload till 4 files. On my case I uploaded one .. and as you can see from var_dump(), the file is up there ... – Tormy Van Cool Apr 11 '18 at 19:33
  • 1
    You seem to be overlooking the obvious and arguing against it. You have *4 form elements*. Whether any file is chosen for any of those, does not matter. That form will POST 4 file fieldsets to PHP. You MUST loop over them all, which is described in that very helpful thread I posted which you flat out ignored. – IncredibleHat Apr 11 '18 at 19:38
  • @IncredibleHat sorry I no want to be polemic. Anyhow I didn't get the solution on the topic you suggested (as I said, I already went on it before to post here). The answer of u_mulder solved my issue perfectly. – Tormy Van Cool Apr 11 '18 at 19:40
  • And the answer here can be obtained from the dupe. It's about how to handle multiple file uploads being an array, which you are trying to do but not quite right – James Apr 11 '18 at 19:54

1 Answers1

5

As you can see from your var_dump your $_FILES['shopImage']['tmp_name'] is array. So you should check elements of this array, and not the array itself. For example:

foreach ($_FILES['shopImage']['tmp_name'] as $tmp_name) {
    if (is_uploaded_file($tmp_name)) {
        echo 'Yes';
    } else {
        echo 'No';
    }
}
u_mulder
  • 54,101
  • 5
  • 48
  • 64