Forum Moderators: coopster

Message Too Old, No Replies

require files in a directory

         

dnb_ovst

7:41 pm on Sep 15, 2006 (gmt 0)

10+ Year Member



Hi guys.

I'm looking for help on requiring all files found in a directory. I came across this script:


<?php
foreach(glob('*'.'.html') as $file) {
echo $file;
}
?>

I had replaced echo $file with require_once $file; but it had thrown an error? I guess require/include does not work with foreach?

Anyone have any insight? Thanks-

Psychopsia

8:25 pm on Sep 15, 2006 (gmt 0)

10+ Year Member



Hi!
Try this:


$dir = '/path/dir';
$fp = @opendir($dir);
while ($file = @readdir($fp))
{
if (preg_match('/.*?\.html/i', $file))
{
include($dir . $file);
}
}
@closedir($fp);

dnb_ovst

9:19 pm on Sep 15, 2006 (gmt 0)

10+ Year Member



Thanks for the reply Psychopsia-

the script worked! Just made one modification to it:


<?php
$dir = '905';
$fp = @opendir($dir);
while ($file = @readdir($fp)) {
if (preg_match('/.*?\.html/i', $file)) {
require_once($dir.'/'.$file); [b]// had to concatenate a '/' between the $dir and the $file variables [/b]
}
}
@closedir($fp);
?>