PHP: How do I echo the contents of all text files in a specified folder?

  • Thread starter Thread starter mti2935
  • Start date Start date
M

mti2935

Guest
using a system call with the cat command might be easier...

$cmd="cat /path/to/folder/*";
$r=`$cmd`; // <--these are backticks, not single quotes
print $r;
 
I have a folder full of text files and I want to echo the contents. Here is my current code. I basically pieced together how to read a folder and how to open a file.

<?php
$dir = './stories/';
$files = scandir($dir);
foreach ($files as &$file) {
if ($file!='.' && $file!='..' )
{

$fp = fopen($file, "r");
$data = fread($fp, filesize($file));
fclose($fp);
$data = nl2br($data);
echo $data;

}
}
?>

How can I get this to work?
 
Back
Top