Listing Files Within a Directory


Displaying Files Within a Directory

The code below is used to display the various css files with a directory.

The code below is checking if a direcory was selected from a form. The $_POST['dir'] was from the input box with the name="dir". If no directory was selected we use getcwd() function to get the current working directory.

if($_POST['dir']){
        $dir = $_POST['dir'];
        }
        else {
        $dir = getcwd();
        //echo $directory;
        }

The code below scans the files in tye directory. The files are in an array. Then, the array is sorted with a foreach loop. If the file name is longer than 3 characters and contains '.css', the file list is output.
$files1 = scandir($dir);
foreach($files1 as $file){
if(strlen($file) >=3){
$foil = strstr($file, 'css'); // As of PHP 5.3.0
//$pos = strpos($file, 'css');
if ($foil==true){
echo $file."<br/>";
}
}
}