tags:

views:

150

answers:

3

Hi, 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 ?

+2  A: 

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.

Pekka
+1  A: 

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.

Pascal MARTIN
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:21Stack 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
redcoder
DO I have to do anything to use the DirectoryIterator ?
redcoder
BTW, I use this code :$dir = new DirectoryIterator("/sellable");foreach ($dir as $file) { if ($file->isDot()) { continue; } echo $file->getFilename() . "\n";}
redcoder
@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' ?
Pascal MARTIN
Yes, your are right martin. I use a wrong directory.. Should be resellable instead of sellable .. That codes is working now .. Thanks ..
redcoder
You're welcome :-) Have fun !
Pascal MARTIN
A: 

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

stmax