Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How can I print all the content of the files in a certain directory using PHP?

Say that I have a folder called "example" and that there are the following files in that folder:

"example1.txt"

"example2.txt"

"example3.txt"

...

...

...

...

"example100.txt"

What do I do to print all the CONTENTS of the text files using PHP?

Thanks.

Update:

what's $dh's original value?

1 Answer

Relevance
  • 1 decade ago
    Favorite Answer

    $directory = "/your_dir/examples/";

    if ($dh = opendir($directory)) {

    // go through each file in the directory

    while (($filename = readdir($dh)) !== false) {

    // open individual file and print its contents

    print file_get_contents($filename);

    }

    closedir($dh);

    }

    $dh is just the data handle. You don't have to worry about its value. It's automatically set when you run the open_dir($directory).

    Source(s): www.php.net
Still have questions? Get your answers by asking now.