通过apache的文件列表问题
我想使用 php exec 函数列出特定目录的文件。我已使用此
exec('ls /home/vikas/hft/a5/traders/sa/*.bin', $NameOfBinaries);
代码列出 /home/vikas/hft/a5/traders/sa/
目录中的 bin 文件。当我在 CLI 模式下运行脚本时它工作正常,但当我在浏览器中运行时它返回空数组。
I want list files of a particular directory using php exec function. I have used this
exec('ls /home/vikas/hft/a5/traders/sa/*.bin', $NameOfBinaries);
code for listing the bin files from /home/vikas/hft/a5/traders/sa/
directory. It works fine when I run the script in CLI mode but when I run in browser it return empty array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Apache 可能无权读取您的主目录中的文件。
为什么不将文件移动到 Apache 可以看到它们的地方并使用 PHP 的 readdir() 函数呢?
http://php.net/manual/en/function.readdir.php
Apache probably doesn't have rights to read files in your home directory.
Why not move the files somewhere that Apache can see them and use PHP's readdir() function?
http://php.net/manual/en/function.readdir.php
您可以使用 glob() PHP 函数来代替,启用错误报告和在第一个错误处停止,如下所示:
请注意:
GLOB_ERR
标志仅适用于 5.1.0 以上的 PHP 版本。此外,这似乎是一个权限问题,因此您可以检查您是否具有对该目录的正确访问权限:
这又是由于 PHP Web 服务器和命令行二进制文件运行时的不同用户造成的,即从命令行运行时,PHP 继承当前登录用户的权限,而当从 Web 运行时,它继承来自 Web 服务器(Apache/ISS 或其他)的用户权限。
为了能够读取该目录(当从网络运行时),必须对该目录设置适当的权限。它必须可由运行 Web 服务器的用户或组读取。
如果您有 Apache 服务器,则在 httpd.conf 中,
User
和Group
指令分别包含 Apache 将在其下运行的用户名和组名。如果您无法访问服务器配置,您应该联系您的系统管理员,请求该目录的读取权限。一个不太安全的选项是将该目录(通过 FTP 或 shell)设置为“世界可读”。
You can use the glob() PHP function instead, enabling error reporting and stopping at first error, like this:
Please note: the
GLOB_ERR
flag works only with PHP versions above 5.1.0.Moreover, this seems a permission issue, so you can check if you have the correct access permissions to the directory:
This, in turn, is due to the different users under which the PHP webserver and commandline binary run, i.e. when run from the commandline, PHP inherits permissions from the currently logged user, while, when run from the web, it inherits user permissions from the webserver (Apache/ISS or whatever).
To be able to read that dir (when run from the web), appropriate permissions must be set on that directory. It must be readable from the user or group under which the webserver runs.
If you have an Apache server, in httpd.conf the
User
andGroup
directives contain respectively the user name and group name under which Apache will run.If you cannot access server config, you should contact your system administrator asking for read permissions of that directory. A less secure option would be to set that directory (via FTP or shell) as "world readable".
确保用户帐户(网络服务器运行的帐户)有权读取该目录。
Make sure that the user-account -- that the webserver runs as -- has permission to read that directory.
系统
怎么样? http://au.php.net/manual/en/function.system.phpHow about
system
? http://au.php.net/manual/en/function.system.php不要为此使用外部函数,而是使用内置 PHP 目录函数< /a>.
Don't use external functions for this, but instead use built-in PHP directory functions.