Hello @kartik,
There's a lot of ways. The older way is scandir but DirectoryIterator is probably the best way.
There's also readdir (to be used with opendir) and glob.
Here are some examples on how to use each one to print all the files in the current directory:
DirectoryIterator usage: (recommended)
foreach (new DirectoryIterator('.') as $file) {
if($file->isDot()) continue;
print $file->getFilename() . '<br>';
}
scandir usage:
$files = scandir('.');
foreach($files as $file) {
if($file == '.' || $file == '..') continue;
print $file . '<br>';
}
opendir and readdir usage:
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if($file == '.' || $file == '..') continue;
print $file . '<br>';
}
closedir($handle);
}
glob usage:
foreach (glob("*") as $file) {
if($file == '.' || $file == '..') continue;
print $file . '<br>';
}
Hope it helps!!
Thank You!!