Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

echo filenames

Posted on 2013-06-12
5
Medium Priority
?
311 Views
Last Modified: 2013-06-19
in windows want to interate through all the files in  a directory
that match file extension .php
and echo file name
0
Comment
Question by:rgb192
5 Comments
 
LVL 12

Accepted Solution

by:
Richard Davis earned 668 total points
ID: 39242678
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
 
LVL 58

Assisted Solution

by:Gary
Gary earned 668 total points
ID: 39242691
<?php
 $dir = "*";
 foreach(glob($dir) as $file)  
 {  
if(pathinfo($file, PATHINFO_EXTENSION)=="php"){
 echo $file ."<br>";  
}
 }
 ?>

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 39242697
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
 
LVL 58

Expert Comment

by:Gary
ID: 39243098
Just to add, if you want a sub directory then amend
$dir = "*";
to
$dir = "mydir/*";
0
 

Author Closing Comment

by:rgb192
ID: 39261185
these loop through the files

thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question