How to Get List of File In Folder In php?

03-Apr-2023

.

Admin

hi Guys,

In this example,I will learn you how to how to get list of file in folder in php. you can esay and simply get list of file in folder in php.

The scandir() function in PHP is an inbuilt function which is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path.

Example 1 :


<?php

$mydir = 'dummy';

$myfiles = array_diff(scandir($mydir), array('.', '..'));

print_r($myfiles);

?>

Output :

Array (

[2] => demo

[3] => demo2

[4] => test1.php

[5] => test2.php

[6] => test3.txt

)

Example 2 :

<?php

$fileList = glob('test/*');

foreach($fileList as $filename){

if(is_file($filename)){

echo $filename, '<br>';

}

}

?>

Output :

dummy/test1.php

dummy/test2.php

dummy/test3.txt

Example 3 :

<?php

$fileList = glob('test/*.php');

foreach($fileList as $filename){

if(is_file($filename)){

echo $filename, '<br>';

}

}

?>

Output :

dummy/test1.php

dummy/test2.php

It will help you....

#PHP