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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitting a sting into elements 6 75
JSON decode 5 46
WordPress:  Reset File Permission after server migration 5 64
Php variable to be sent back 3 35
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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 count occurrences of each item in an array.

751 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