echo filenames

in windows want to interate through all the files in  a directory
that match file extension .php
and echo file name
LVL 1
rgb192Asked:
Who is Participating?
 
Richard DavisConnect With a Mentor Senior Web DeveloperCommented:
Try something like this;

<?php
$dir_path = '\\path\\to\\directory';
if ($handle = opendir($dir_path)) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && preg_match('/\.php$/im', $entry)) {
            echo "$entry\n";
        }
    }
    closedir($handle);
}
?>

Open in new window


Have not tested this, but hope it helps.

~AB
0
 
GaryConnect With a Mentor Commented:
<?php
 $dir = "*";
 foreach(glob($dir) as $file)  
 {  
if(pathinfo($file, PATHINFO_EXTENSION)=="php"){
 echo $file ."<br>";  
}
 }
 ?>

Open in new window

0
 
Ray PaseurConnect With a Mentor Commented:
Have a look at scandir() and glob().  Both will return an array. You can use an iterator like foreach() to access each element of the array.
0
 
GaryCommented:
Just to add, if you want a sub directory then amend
$dir = "*";
to
$dir = "mydir/*";
0
 
rgb192Author Commented:
these loop through the files

thanks
0
All Courses

From novice to tech pro — start learning today.