how to scan a directory with this code

Posted on 2007-08-02
Last Modified: 2008-02-01

//$a = scandir("/path/to/dir");
$a = scandir(".");
$f = create_function('$v', 'return preg_match("/.*\.htm$/", $v);');
$a = array_values(array_filter($a, $f));
// $a[0] now contains the file name
//$file_name = "*.htm";
$file_name = $a[0];

$html = implode('', file($file_name)) or die("Cant open file!");

id like to scan all the sub directory's too

unless you know a better way to write this, im open

thank you in advance for any code and help you may provide
Question by:Johnny
    LVL 11

    Accepted Solution

    The way i see it in your exampe is that you just need a list of all HTM files in the directory and subdirectories without knowing exactly where in the tree the file is.

    Here's your code rearanged to fulfill recursive directory scan requirement:

    function ListHTML($path){
       $a = scandir($path);
       foreach($a as $key => $name){
          if($name != "." && $name != ".."){
             } else {
                   echo $name."<br>";

    Author Comment

    thank you

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now