user2869163 user2869163 - 1 month ago 10
PHP Question

Trying to include files within subdirectories in php but it is showing errors

I am trying to recursively loop through a bunch of subdirectories and include the files within them instead of 'hardcoding' them in, the problem is if I do hardcode the include path it displays correctly but using the below code I keep getting 'failed to open stream: No such file or directory'. I have tried many different approaches and keep getting similar errors. If anybody has a suggestion of what or where I am going wrong I would appreciate it.

include path would be like: (/core/edit_object/border/border_radius.php);

filesInDir('core/edit_object');
function filesInDir($tdir) {
$dirs = scandir($tdir);
foreach($dirs as $file) {
if (($file == '.') || ($file == '..') || ($file == '.DS_Store')) {
} else if (is_dir($tdir.'/'.$file)) {
filesInDir($tdir.'/'.$file);
} else {
//echo '/'.$tdir.'/'.$file."<br>";
include '/'.$tdir.'/'.$file;
}
}
}

Answer

Decided I would try a different approach, I found after running the code below that it was returning what I was after although with many many include errors as in my original post. I found after turning off error reporting in php.ini it ran perfectly. Thank to the posters above with their help and suggestions.

$dir = new RecursiveDirectoryIterator('test');
foreach (new RecursiveIteratorIterator($dir) as $filename => $file) {
$search  = array('.DS_Store');
$file = str_replace($search, '', $file);
include $file;
}
Comments