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 DavisSenior 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
 
GaryCommented:
<?php
 $dir = "*";
 foreach(glob($dir) as $file)  
 {  
if(pathinfo($file, PATHINFO_EXTENSION)=="php"){
 echo $file ."<br>";  
}
 }
 ?>

Open in new window

0
 
Ray PaseurCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.