Search code examples
phpforeachphpmailer

PHP foreach stopped working after some time


This PHP foreach loop is supposed to loop through all the images that had been just uploaded to a directory from a form and then attach them all to an email and display them in email. It worked when I first made it a couple of months ago but now for some reason it is no longer working.. It will only attach one picture..

$files = array();
while ($files[] = readdir($dir2));
closedir($dir2);

foreach ($files as $file) {

    //MANIPULATE FILENAME HERE, YOU HAVE $file...

    if ($file != "." && $file != ".." && $file != 'resources' ){
        $withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $file);
        $url = '../mobile_patrol_reports/'.$id.'/'.$file;
        $mail->AddEmbeddedImage($url, $withoutExt);
        $mail->Body .= '<img src="cid:'.$withoutExt.'">';
        break;
    }

}

Maybe it was something to do with adding an SSL cert and I could be missing something?


Solution

  • It has something to do with break; that you have in your foreach loop. The first time the condition in if is true, break will cause to abort the execution of the foreach.

    http://php.net/manual/en/control-structures.break.php