1

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;
        }
    }
}
  • Try to debug the variable $file to see how the path is formed so you can see you are not having issues with trailing slashes. – Dez Oct 02 '16 at 09:10
  • Hi Dez, this is what I am getting. Warning: include(/core/edit_object/border/border_radius.php): failed to open stream: No such file or directory in /Library/WebServer/Documents/index.php on line 77. There doesn't seem to be any trailing slashes on $file. – user2869163 Oct 02 '16 at 09:15
  • 1
    Follow the steps of the accepted answer of this question: https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory/36577021 You will be able to find the problem. – Dez Oct 02 '16 at 09:21
  • remove first `/` from this line : `include '/'.$tdir.'/'.$file;` – Saeed M. Oct 02 '16 at 11:29

1 Answers1

0

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;
}