0

Previously I wrote a code to get all images from a folder via this code down. In this version "image_folder" comes from user. When user clicks a button it gets the same name and the same folder and finding all images related with it.

<?php
if (isset($_GET['grand'])) {
    $grand = $_GET['grand'];
    $image_folder = "images/products/categories/".$grand;
    $desc = $grand;

    if (isset($_GET['father'])){
        $father = $_GET['father'];
        $image_folder = "images/products/categories/".$grand."/".$father;
        $desc = $father;

        if (isset($_GET['child'])){
            $child = $_GET['child'];
            $image_folder = "images/products/categories/".$grand."/".$father."/".$child;
            $desc = $child;
        }
}
/* Show category name */ ?>
<h1><?php echo $desc; ?></h1> 

<?php
    $all_files = glob($image_folder."/*.*");
    for ($i=0; $i<count($all_files); $i++){
        $image_url = $all_files[$i];
        //Get image name
        $image_name = pathinfo($image_url, PATHINFO_FILENAME);
        //Get image name
        $supported_format = array('gif','jpg','jpeg','png');
        $ext = strtolower(pathinfo($image_url, PATHINFO_EXTENSION));
        if (in_array($ext, $supported_format)){ ?>

            <img src="<?php echo $image_url; ?>">                       

        <?php } else { continue; }//Continue scan ?>
<?php }//End ?>
<?php } else { ?>
<h2>Please Choose Category</h2>
<?php } ?>

This time i just have a more complicated version of it and I'm a bit stuck :) I want to list every image in a folder.

  • Products (folder)
    • 1001 (folder)
      • 1001_01_1.jpg
      • 1001_01_2.jpg
      • 1001_01_3.jpg
      • 1001_02_1.jpg
      • 1001_02_2.jpg
      • 1001_02_3.jpg
    • 1002 (folder)
      • 1002_01_1.jpg
      • 1002_01_2.jpg
      • 1002_01_3.jpg
      • 1002_01_4.jpg
      • 1002_02_1.jpg
      • 1002_02_2.jpg
    • 1003 (folder)
      • 1003_01_1.jpg
      • 1003_01_2.jpg
      • 1003_02_1.jpg
      • 1003_02_2.jpg
      • 1003_02_3.jpg
      • 1003_02_4.jpg

This means I have bunch of products and bunch of images for them. And this time user won't click and i just want to sort all of them as image list. 1001 is the product name, 01-02 are color names and the last part 1-2-3-4 are just image names. I'm trying to make a catalogue :)

My aim is:

  • Only get first image for each color from each folder: "1001_01-1.jpg"
  • Get all images from each folder.
  • Get first part of the image name: "1001_01"
  • And i need to use them in a loop for listing as images (for, foreach)

Any ideas will help me so much. Thank you all <3

Mad Gemini
  • 23
  • 9
  • I'm checking now – Mad Gemini May 01 '23 at 01:32
  • Well, you already have your implementation that finds all files. Now all you have to do is add all found files to an array and sort that however you like. So what is your actual question? – arkascha May 01 '23 at 06:56
  • I just want to get only image files and list only the first one for all of them – Mad Gemini May 01 '23 at 11:23
  • @KenLee thanks for the link. I've just checked and tested many codes in that topic. That topic gives many information but my question is a bit different. I can list directories and sub directories even files. But i don't know how to parse or use them. I need some more help to find my solution. It may be about my low level php knowledge :) – Mad Gemini May 01 '23 at 12:52

0 Answers0