List Files In A Directory

Posted by Pete | Posted in PHP Functions | Posted on 10-08-2009-05-2008

0

In this tutorial we will learn how to list all the files that are in a directory via PHP. This is a quick and easy technique which will also remove the . and .. from the listings. So lets get started.

<?php
$dir = "."; // . is the current directory you can change this to what ever directory you want to be listed e.g public_html/
if($scan = scandir($dir)){ // Assign scandir function to $scan and check if it was run.
	foreach ($scan as $value){ // Now the directory has been scanned go through the array and assign the values in the array to $value.
		if($value != "." && $value != ".."){ // Check if $value == . or .. and if they do dont echo it.
			echo $value."<br />"; // Echo $value
		}
	}
}
?>

And that’s it. It really is simple for more information about the scandir function visit http://uk3.php.net/manual/en/function.scandir.php. If you want any more help or want to leave feedback please leave a comment.

Write a comment

You must be logged in to post a comment.

Tutorials Written By Peter Kelly