Retrieving file names in a directory

What you need to do to get the headers (for example, abc.jpg) of images from a folder / directory using PHP and save them in an array.

For example:

a[0] = 'ac.jpg' a[1] = 'zxy.gif' 

and etc.

I will use an array in a slide show.

+7
source share
4 answers

It is certainly possible. See the documentation for opendir and push each file into an array of results. If you are using PHP5, check out DirectoryIterator . This is a much smoother and cleaner way to move directory contents!

EDIT: Creating on opendir:

 $dir = "/etc/php5/"; // Open a known directory, and proceed to read its contents if (is_dir($dir)) { if ($dh = opendir($dir)) { $images = array(); while (($file = readdir($dh)) !== false) { if (!is_dir($dir.$file)) { $images[] = $file; } } closedir($dh); print_r($images); } } 
+12
source

'scandir' does the following:

 $images = scandir($dir); 
+5
source

One insert: -

 $arr = glob("*.{jpg,gif,png,bmp}", GLOB_BRACE) 
+5
source

glob in php - Find paths matching pattern

 <?php //path to directory to scan $directory = "../images/team/harry/"; //get all image files with a .jpg extension. This way you can add extension parser $images = glob($directory . "{*.jpg,*.gif}", GLOB_BRACE); $listImages=array(); foreach($images as $image){ $listImages=$image; } ?> 
+4
source

All Articles