Search code examples
phpimagefilecolorsimagefilter

Read the content of directory in php


please i am beginner in php and i want to read images in a folder I have pictures with names like:

image_1_0_0_0.png
image_1_1_0_0.png
image_1_1_1_0.png
.........
.....
image_2_0_0_0.png
image_2_1_0_0.png

image_3_0_0_0.png

I want to assign a color (imagefilter) to the images that starts with image_1 _..... and another color for image_2 _... and another color to images_3 ....

then I want to read only the last image and retrieve only the number 3, the first number left

I want to know how to do this please.


Solution

  • To read the content of a directory:

    $dir = "/images/";
    $color = "#000000";
    
    // Open a directory, and read its contents
    if (is_dir($dir)){
      if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){
          echo "Filename: ".$file."<br>";
          if($file[7]=='1') $color="#FF0000";
          elseif ($file[7]=='2') $color="#00FF00";
          elseif ($file[7]=='3') $color="#0000FF";
          else $color="#000000";
          // do something with the image
        }
        closedir($dh);
      }
    }
    

    I don't understand your last question.