SQL PHP HTML ASP JavaScript articles and free scripts to download
 

PHP directory listing

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. Read the path and the script will take care to list the files inside that. Here is the code.

// open the current directory by opendir
$handle=opendir(".");

while (($file = readdir($handle))!==false) {
echo "$file <br>";
}

closedir($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);




Further readings
PHP file handling functions
Reading from local and remote files by using fopen()
Writing to a file by fwrite() function
file_exists: Checking if file is present or not
unlink:Deleting file by using unlink function
Deleting all files present inside a directory
Downloading files using header control
File upload to server using PHP
File upload using PHP 5 and register_global off
Uploading More than one file to server
Displaying all files and directory of a folder
Getting the Present file name running the PHP script
pathinfo: getting all the details like dir name, file name , extension etc
Getting the last updated time of the file in PHP
Displaying the last modification time of the file
Searching for text within a pair of tags of a html page
Listing All title tags inside a directory
Creating thumbnail images after uploading the file
















Join Our Email List
Email:  
For Email Newsletters you can trust
File handling
PHP Sections