echo filenames

Posted on 2013-06-12
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
Question by:rgb192
LVL 12

Accepted Solution

adrian_brooks earned 167 total points
ID: 39242678
Try something like this;

$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";

Open in new window

Have not tested this, but hope it helps.

LVL 58

Assisted Solution

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

Open in new window

LVL 110

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.
LVL 58

Expert Comment

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

Author Closing Comment

ID: 39261185
these loop through the files


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 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