Listing all images in a directory using PHP

前端 未结 8 758
悲&欢浪女
悲&欢浪女 2021-01-04 08:39

I have the code below that lists all the images in a folder, the problem is that it finds some files ( a . and a ..) that I am not sure what they are so I a

8条回答
  •  旧时难觅i
    2021-01-04 08:50

    To get all jpg images in all dirs and subdirs inside a folder:

    function getAllDirs($directory, $directory_seperator)
    {
    
    $dirs = array_map(function ($item) use ($directory_seperator) {
        return $item . $directory_seperator;
    }, array_filter(glob($directory . '*'), 'is_dir'));
    
    foreach ($dirs AS $dir) {
        $dirs = array_merge($dirs, getAllDirs($dir, $directory_seperator));
    }
    return $dirs;
    }
    
    
    
    function getAllImgs($directory)
     {
      $resizedFilePath = array();
      foreach ($directory AS $dir) {
    
        foreach (glob($dir . '*.jpg') as $filename) {
    
            array_push($resizedFilePath, $filename);
    
        }
    
    }
      return $resizedFilePath;
    }
    
    
     $directory = "C:/xampp/htdocs/images/";
     $directory_seperator = "/";
    
     $$allimages = getAllImgs(getAllDirs($directory, $directory_seperator));
    

提交回复
热议问题