PHP directory listing

$handle=opendir("."); // open the current directory
$handle=opendir("E:\\my_dir\\"); // OR use the path 

while (($file = readdir($handle))!==false) {
echo "$file <br>";
}
closedir($handle);
Many times we have to display the list of files in a directory. We can keep the script in any location and by using the file path we can display the files inside the directory.

opendir():
Open the directory handle, returns False on failure.
readdir():
Read entry from directory handle.
closedir():
Close directory handle

Reading and displaying all images present inside a directory

We can use the same code to read all the files in a directory and assuming that all are images, we can display them. Here is the code to do this.
$path='images/';// change the path here related to this page
$handle=opendir($path);

while (($file = readdir($handle))!==false) {
if(strlen($file)>3){echo "<img src=$path$file> <br>";}
}
closedir($handle);
You can also list the files and directories by using dir function scandir()

Directory Functions
PHP File handling
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    06-07-2021

    Superb

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer