开发者

exec function doesn't work

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 o开发者_运维知识库n 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 ?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜