Sunday, August 2, 2009

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.

How can I print all the content of the files in a certain directory using PHP?
$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).


No comments:

Post a Comment