1

This all worked under an older version of PHP, and I know exactly where and what the problem is. Just not sure how to fix it.

I have a form that has other forms of input but this is the part I need help with.

Attachment(s):<br> <input type="file" name="uploadfile[]"/><br>
               <input type="file" name="uploadfile[]"><br>
               <input type="file" name="uploadfile[]"><br>

And the code that deals with the file inputs:

$total = count($_FILES['uploadfile']['tmp_name']);
    for ($i = 0; $i = $total; $i++) {
    $name = $_FILES['uploadfile']['name'][$i];
   $path = $_FILES['uploadfile']['tmp_name'][$i];
   $mail->AddAttachment($path,$name);
}

Now the problem is, unless all three inputs from the form have a file in them, it throws an error if there is only one file it gives:

"could not access file. Could not access file". (2 errors)

If there are two files it gives:

"Could not access file" (1 error)

and if there are three files there is no error.

So I know it is in the count() function that is the issue

  • if you use a foreach() loop you dont have to count –  Dec 29 '15 at 23:39
  • When I use foreach, for some reason none of the files attach. – Stephen Thompson Dec 30 '15 at 01:02
  • `print_r($_FILES);` lets see what your actually dealing with –  Dec 30 '15 at 01:55
  • Do what Dagon says, but also read the docs on handling file uploads because your code is not safe. – Synchro Dec 30 '15 at 05:53
  • @Dagon This is what prints: 'Could not access file: Array Array ( [uploadfile] => Array ( [name] => Array ( [0] => BMS_Logo_profile.png [1] => [2] => ) [type] => Array ( [0] => image/png [1] => [2] => ) [tmp_name] => Array ( [0] => /tmp/phpevvVWm [1] => [2] => ) [error] => Array ( [0] => 0 [1] => 4 [2] => 4 ) [size] => Array ( [0] => 189263 [1] => 0 [2] => 0 ) ) )' – Stephen Thompson Dec 30 '15 at 18:23

0 Answers0