Asked  7 Months ago    Answers:  5   Viewed   34 times

I have this working function that finds folders and creates an array.

function dua_get_files($path)
{
    foreach (glob($path . "/*", GLOB_ONLYDIR) as $filename)
    {
        $dir_paths[] = $filename;   
    }
    return $dir_paths;
}

This function can only find the directories on the current location. I want to find the directory paths in the child folders and their children and so on.

The array should still be a flat list of directory paths.

An example of how the output array should look like

$dir_path[0] = 'path/folder1';
$dir_path[1] = 'path/folder1/child_folder1';
$dir_path[2] = 'path/folder1/child_folder2';
$dir_path[3] = 'path/folder2';
$dir_path[4] = 'path/folder2/child_folder1';
$dir_path[5] = 'path/folder2/child_folder2';
$dir_path[6] = 'path/folder2/child_folder3';

 Answers

19

If you want to recursively work on directories, you should take a look at the RecursiveDirectoryIterator.

$path = realpath('/etc');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object){
    echo "$namen";
}
Wednesday, March 31, 2021
 
juanrpozo
answered 7 Months ago
12

This note on the manual page of glob() seems to answer your question, saying that glob is not limited to a single directory : using GLOB_BRACE, you can specify several directories.


I'm quoting the example that @Ultimater gives there :

$results=glob("{includes/*.php,core/*.php}",GLOB_BRACE);


User-notes on the manual pages often contain useful informations and examples ;-)

Wednesday, March 31, 2021
 
MassiveAttack
answered 7 Months ago
96

Check out the resource at http://www.xdebug.org/docs/install#configure-php and make sure you have everything configured as-described.

Specifically:

  1. Ensure that your debugger is set up correctly in php.ini
  2. Uncomment the string in php.ini
  3. Restart your web-server (if you are on modphp)
  4. Make sure that in your phpinfo() you have set xdebug
Wednesday, March 31, 2021
 
IvanH
answered 7 Months ago
17
if(glob("images/*.{jpg,png,gif}", GLOB_BRACE))
{
  //create gallery
}

And that's about it :)

Wednesday, March 31, 2021
 
Skipper
answered 7 Months ago
55

I would suggest you look at the MVC Design Pattern so that you understand it before you start using it.

Ideally, you should already have made some projects where you suffered a little bit because you wanted to change stuff when your project was at least halfway done.(This is one of the main motivators behind a lot of so-called advanced programming techniques. Ease of modification, flexibility and so on).

This is where stuff like what you mentioned (OO,MVC, and I would add TDD, Design Patterns) all come in).

What I do for myself and suggest others do is the following. Look into whatever you want, be it TDD, MVC or any other advanced technique. But wait until you need them.

In my personal opinion, one can much better appreciate the value of OO and MVC when one has gone through a naïve project and suffered from lack of functionality these techniques provide...

The golden rule is.. only start using a new technique when you've understood it and seen what benefits it can bring.

I say that because what I see in real life is that many people use stuff like OO, MVC and even Design Patterns without understanding the why's behind. I don't think that helps at all.

Good luck.

Saturday, May 29, 2021
 
Sufi
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :