I want to read a list the names of files in a folder in a web page using php. is there any simple script to acheive it?
The scandir() function returns an array of files and directories of the specified directory.
PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.
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.
The simplest and most fun way (imo) is glob
foreach (glob("*.*") as $filename) { echo $filename."<br />"; }
But the standard way is to use the directory functions.
if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: .".$file."<br />"; } closedir($dh); } }
There are also the SPL DirectoryIterator methods. If you are interested
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With