Solved

echo filenames

Posted on 2013-06-12
5
303 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:
adrian_brooks earned 167 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 167 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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Download tables into separate sheets 3 25
ebay devID, appID, certID, userToken 2 25
I wonder how people fake their ip address? 3 37
Checkout Page Input Field not aligned 1 21
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

776 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