Get file name from directory 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

GordonGordon

307k72 gold badges525 silver badges550 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.


Taryn

237k55 gold badges362 silver badges402 bronze badges

answered Oct 22, 2013 at 8:55

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

How can I get all filenames in a directory in PHP?

Approach: In order to get all the files from the particular directory, we need to specify the complete path of the file & store the path value in the variable as $mydir. Then use the scandir[] function that will scan for the files in a current or specific directory & return an array of files and directories.

What is __ DIR __ in PHP?

The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname[__FILE__]. Usually, it is used to include other files that is present in an included file.

What is $filename in PHP?

The basename[] function is an inbuilt function that returns the base name of a file if the path of the file is provided as a parameter to the basename[] function. Syntax: $filename = basename[path, suffix];

How can check file in folder in PHP?

Summary.
Use the file_exists[] function to check if a file exists..
Use the is_file[] function to check if a path is a regular file, not a directory, and that file exists..
Use the is_readable[] function to check if a file exists and readable..
Use the is_writable[] function to check if a file exists and writable..

Chủ Đề