Get image name from folder in php

I have been trying to figure out a way to list all files contained within a directory. I'm not quite good enough with php to solve it on my own so hopefully someone here can help me out.

I need a simple php script that will load all filenames contained within my images directory into an array. Any help would be greatly appreciated, thanks!

asked Jan 13, 2010 at 20:13

0

Try glob

Something like:

 foreach(glob('./images/*.*') as $filename){
     echo $filename;
 }

Anthony

35.6k24 gold badges93 silver badges159 bronze badges

answered Jan 13, 2010 at 20:17

a.yastreba.yastreb

1,4931 gold badge10 silver badges11 bronze badges

4

scandir() - List files and directories inside the specified path

$images = scandir("images", 1);
print_r($images);

Produces:

Array
(
    [0] => apples.jpg
    [1] => oranges.png
    [2] => grapes.gif
    [3] => ..
    [4] => .
)

answered Jan 13, 2010 at 20:14

SampsonSampson

261k74 gold badges530 silver badges559 bronze badges

2

Either scandir() as suggested elsewhere or

  • glob() — Find pathnames matching a pattern

Example

$images = glob("./images/*.gif");
print_r($images);

/* outputs 
Array (
   [0] => 'an-image.gif' 
   [1] => 'another-image.gif'
)
*/

Or, to walk over the files in directory directly instead of getting an array, use

  • DirectoryIterator — provides a simple interface for viewing the contents of filesystem directories

Example

foreach (new DirectoryIterator('.') as $item) {
    echo $item, PHP_EOL;
} 

To go into subdirectories as well, use RecursiveDirectoryIterator:

$items = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('.'),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach($items as $item) {
    echo $item, PHP_EOL;
}

To list just the filenames (w\out directories), remove RecursiveIteratorIterator::SELF_FIRST

answered Jan 13, 2010 at 20:14

Get image name from folder in php

GordonGordon

307k72 gold badges526 silver badges551 bronze badges

2

You can also use the Standard PHP Library's DirectoryIterator class, specifically the getFilename method:

 $dir = new DirectoryIterator("/path/to/images");
 foreach ($dir as $fileinfo) {
      echo $fileinfo->getFilename() . "\n";
 }

answered Jan 13, 2010 at 20:26

AnthonyAnthony

35.6k24 gold badges93 silver badges159 bronze badges

2

This will gives you all the files in links.

".$count.". ";
        echo "".$filename."
";
        $count++;
    }
}
?>

Get image name from folder in php

Taryn

237k55 gold badges362 silver badges402 bronze badges

answered Oct 22, 2013 at 8:55

Get image name from folder in php

HardikHardik

1,27314 silver badges34 bronze badges

Maybe this function can be useful in the future. You can manipulate the function if you need to echo things or want to do other stuff.

$wavs = array();
$wavs = getAllFiles('folder_name',$wavs,'wav');

$allTypesOfFiles = array();
$wavs = getAllFiles('folder_name',$allTypesOfFiles);

//explanation of arguments from the getAllFiles() function
//$dir -> folder/directory you want to get all the files from.
//$allFiles -> to store all the files in and return in the and.
//$extension -> use this argument if you want to find specific files only, else keept empty to find all type of files.

function getAllFiles($dir,$allFiles,$extension = null){
    $files = scandir($dir);
    foreach($files as $file){           
        if(is_dir($dir.'/'.$file)) {
            $allFiles = getAllFiles($dir.'/'.$file,$allFiles,$extension);
        }else{
            if(empty($extension) || $extension == pathinfo($dir.'/'.$file)['extension']){
                array_push($allFiles,$dir.'/'.$file);
            }
        }
    }
    return $allFiles;
}

answered Aug 29, 2013 at 20:02

1