1

I have a class that loops through a directory (5 images) and converts each image to base64 format and fills an array. However, it seems that the foreach loop only loops through once. There is 5 images in the directory, so it should have 5 iterations and the array should be 5 different images as well.

PHP

require_once "Results.php";
require_once "ImageHelper.php";

class IntroImageHelper {
    public static function GetImages()
    {
      $results = new Results();
      $results->IntroImages = Array();
      $dir = new DirectoryIterator("img/");
      $ImageExists = false;
      foreach($dir as $file)
      {
        if($file->isFile())
        {
          $ImageExists = $file->__toString();
          break;
        }
      }

      if($ImageExists)
      {
        $tempImage = new Results();
        $tempImage->ImageName = $ImageExists;
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
      }

       return $results;
    }
}

Output:

{"IntroImages: [
    {"ImageName": "image.png",
     "ImageDate": "base64imagedata"
    }
  ]
}
1
  • You're not calling array_push() inside the loop. Commented Dec 8, 2014 at 22:13

3 Answers 3

4

Using a break statement will stop loops. You can use continue to skip the rest of the execution of the current iteration and go to to the next iteration, but in your case just omitting the break will fix your problem:

foreach($dir as $file) {
    if($file->isFile()) {
      $ImageExists = $file->__toString();
    }
}

In your case, a bunch of your code is outside of the loop when it should be inside:

foreach($dir as $file) {
    if($file->isFile()) {
      $ImageExists = $file->__toString();
    }
    if($ImageExists) {
        $tempImage = new Results();
        $tempImage->ImageName = $ImageExists;
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}

But you can also combine those two statements:

foreach($dir as $file) {
    if($file->isFile()) {
        $tempImage = new Results();
        $tempImage->ImageName = $file->__toString();
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Scopey! I didnt fully grasp the logic of a break. I assumed that it would temporarily stop, run the rest of the code, then come back and finish the loop.
0

That's exactly what break will do, it will break out of the loop.

If you need to just flag a value, remove the break and let the iterations continue.

But in your case you did not need to, for a check change your loop to the following:

foreach($dir as $file)
{
    if($file->isFile())
    {
        $tempImage = new Results();
        $tempImage->ImageName = $file->__toString();
        $tempImage->ImageData = ImageHelperIntroSlides::DownloadImage($file);
        array_push($results->IntroImages, $tempImage);
    }
}

Comments

0

Why do you have the break in the for loop? That will quit the loop...

2 Comments

This is a question, not an answer.
it's a question-answer. It only goes once through because of the break. I was trying to point that out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.