exec 函数不起作用
我从 http://kevin.vanzonneveld.net/techblog/article/convert_anything_to_tree_structs_in_php/ 找到了这段代码,但我无法让它工作。我正在 WIndow 环境中工作,我使用的路径是 /sellable,其中可销售是工作文件夹内的文件夹:
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);
}
任何人都可以帮助我吗?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Windows 计算机上,您几乎不会获得
find
命令,也不会获得/etc/php5
目录。使用 PHP 的内置 glob 或 < Strike>DirectoryIterator RecursiveDirectoryIterator (谢谢帕斯卡:)。 Glob 本身无法迭代子文件夹,但链接页面上的用户贡献注释中有简单的globr
实现。迭代器可以本机执行此操作。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 theDirectoryIteratorRecursiveDirectoryIterator (Thanks Pascal :) instead. Glob can't iterate through sub-folders natively, but there are simpleglobr
implementations in the User Contributed Notes on the linked page. The iterator can do this natively.find
是一个 Linux 命令(外部 Linux 程序)。这意味着它不会出现在 Windows 上...
并且
/etc/php5
确实看起来像目录的 UNIX 路径;并且看起来不像 Windows 目录的路径。所以,这里有两个问题:
find
。但我想说仅 PHP 的解决方案可能会更好:有一些函数和类允许您搜索文件并迭代文件系统 - 并且它可以在 Linux 和 Linux 上运行Windows,不依赖于任何外部程序。
例如,要迭代目录,您可能需要查看
RecursiveDirectoryIterator
类 - 也许还有DirectoryIterator
。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 :
find
.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 alsoDirectoryIterator
.我还没有尝试过,但“dir /s /bc:\somedir”可能可以作为 Windows 上“find”的快速替代品。更好(且更便携)的解决方案是使用 RecursiveDirectoryIterator 或 php 的 opendir/readdir 函数来递归列出目录中的所有文件。
请参阅此处的示例代码,例如:
http://php.net/manual/en/function.readdir.php
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