0

I found this code from http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structures_in_php/, but I couldn't get it work. I am working on WIndow environment and the path I use is /sellable where the sellable is the folder inside the working folder :

if(exec("find /etc/php5", $files)){
    // the $files array now holds the path as it's values,
    // but we also want the paths as keys:
    $key_files = array_combine(array_values($files), array_values($files));

    // show the array
    print_r($key_files);
}

Can anyone help me ?

15
  • Iterator seems fine with me .. I manage to get the directory and subdirectory ... But my problem is that, I couldn't get the value of the Iterator object.. I can only print it out on screen.. If I use $file->getFilename() , it print on screen, however is I assign it to var, $file_name = $file->getFilename() , the var value is empty.. Why is that ? Commented Feb 25, 2010 at 20:50
  • Sorry, I have to re-open this question .... I use the RecursiveIteratorIterator which works fine in my local machine Windows as well as Linux on 3rd party server.. Unfortunately, when I move to my client's server (OSCent) , the recursive doesn't works well ...My clients PHP is a Version 5.1.6 .. Could this be the root cause ? Commented Mar 9, 2010 at 22:52
  • In Windows env, I got these results : Array ( [folder1] => folder1 [folder1\folder1.1] => folder1\folder1.1 [folder1\folder1.1\abc.txt] => folder1\folder1.1\abc.txt [folder1\folder1.1\notes.txt] => folder1\folder1.1\notes.txt [folder1\folder1.2] => folder1\folder1.2 [folder1\folder1.2\abc.txt] => folder1\folder1.2\abc.txt [folder1\folder1.2\iphone icard.txt] => folder1\folder1.2\iphone icard.txt [folder1\folder1.2\Logo Saloon Rose House Site.pdf] => folder1\folder1.2\Logo Saloon Rose House Site.pdf Commented Mar 9, 2010 at 22:54
  • [folder2] => folder2 [folder2\folder2.1] => folder2\folder2.1 [folder2\folder2.1\notes.txt] => folder2\folder2.1\notes.txt [folder2\folder2.2] => folder2\folder2.2 [folder2\folder2.2\iphone icard.txt] => folder2\folder2.2\iphone icard.txt [folder2\folder2.2\php_readfile.txt] => folder2\folder2.2\php_readfile.txt [folder3] => folder3 [folder3\folder3.1] => folder3\folder3.1 [folder3\folder3.1\3.odt] => folder3\folder3.1\3.odt [folder3\folder3.1\iphone icard.txt] => folder3\folder3.1\iphone icard.txt Commented Mar 9, 2010 at 22:54
  • [folder3\folder3.1\notes.txt] => folder3\folder3.1\notes.txt [folder3\folder3.2] => folder3\folder3.2 [folder3\folder3.2\iphone icard.txt] => folder3\folder3.2\iphone icard.txt [New Folder] => New Folder [New Folder\folder4] => New Folder\folder4 [New Folder\folder4\notes.txt] => New Folder\folder4\notes.txt [New Folder\folder4\PhotoPlus Reg.txt] => New Folder\folder4\PhotoPlus Reg.txt Commented Mar 9, 2010 at 22:55

3 Answers 3

2

You are hardly going to get the find command, nor a /etc/php5 directory on a windows machine. Use PHP's built-in glob or the DirectoryIterator RecursiveDirectoryIterator (Thanks Pascal :) instead. Glob can't iterate through sub-folders natively, but there are simple globr implementations in the User Contributed Notes on the linked page. The iterator can do this natively.

Sign up to request clarification or add additional context in comments.

Comments

1

find is a Linux command (an external Linux program).
Which means it will not be present on windows...

And /etc/php5 really looks like an UNIX path to a directory ; and doesn't look like a path to a Windows directory.

So, two problems here :

  • You have to find an equivalent of... find.
    • Maybe using something like cygwin ?
  • You have to adapt the path, so it fits your system


But I'd say that a PHP-only solution would probably be better : there are functions and classes that will allow you to search files and iterate over the filesystem -- and it would work on both Linux and Windows, not depending on any external program.

For instance, to iterate over a directory, you might want to take a look to the RecursiveDirectoryIterator class -- and maybe also DirectoryIterator.

5 Comments

Hi Martin, I got this from php error log.. [25-Feb-2010 20:23:14] PHP Fatal error: Uncaught exception 'RuntimeException' with message 'DirectoryIterator::__construct(\sellable) [<a href='directoryiterator.--construct'>directoryiterator.--construct</a>]: failed to open dir: No such file or directory' in C:\wamp\www\waterwell\display_e_book.php:21 Stack trace: #0 C:\wamp\www\waterwell\display_e_book.php(21): DirectoryIterator->__construct('\sellable') #1 C:\wamp\www\waterwell\e_book.php(40): include('C:\wamp\www\wat...') #2 {main} thrown in C:\wamp\www\waterwell\display_e_book.php on line 21
DO I have to do anything to use the DirectoryIterator ?
BTW, I use this code : $dir = new DirectoryIterator("/sellable"); foreach ($dir as $file) { if ($file->isDot()) { continue; } echo $file->getFilename() . "\n"; }
@redcoder : like the error message says : "No such file or directory" ;;; which means you are probably not specifying the right path as a parameter when instanciating DirectoryIterator ; I suppose /sellable is not a valid directory ; maybe you have to specify something like C:/Users/.../sellable ; or use a relative path, like '../sellable' ?
Yes, your are right martin. I use a wrong directory.. Should be resellable instead of sellable .. That codes is working now .. Thanks ..
0

i haven't tried it, but "dir /s /b c:\somedir" might work as a quick replacement for "find" on windows. a better (and more portable) solution would be to use the RecursiveDirectoryIterator or php's opendir/readdir functions to recursively list all files in a directory.

see example code here for example: http://php.net/manual/en/function.readdir.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.