使用 opendir() 扫描 /home/
是否可以使用 opendir 和 scandir 扫描 /home/ 目录。当我尝试执行脚本时,它说权限被拒绝,我该怎么办?
<?php
$dir = '/home/';
$dirs = scandir($dir);
?>
<pre>
<?php print_r($dirs); ?>
</pre>
Is it possible to scan /home/ directory with opendir and scandir. When i try to exec script it says permission denied what should i do?
<?php
$dir = '/home/';
$dirs = scandir($dir);
?>
<pre>
<?php print_r($dirs); ?>
</pre>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
is_read('/home/')
来检查您是否有权限。如果没有,您需要确保该目录具有读取权限,可能是0755
(rwxr-xr-x)You can use
is_readable('/home/')
to check if you have permission. If not you'd need to make sure the directory has read privileges, probably0755
(rwxr-xr-x)为了安全起见,PHP 定义了一个“basedir”,低于该目录的用户不允许访问。正如 Aleks G 所说,还需要考虑文件权限。
这个问题讨论了如何绕过basedir限制: How can I Relax PHP's open_basedir 限制?
Tom Haigh 的答案复制到此处:
您还可以使用 Apache(假设这是您的 Web 服务器)配置文件(例如 httpd.conf)在每个目录上轻松完成此操作,
您也可以完全消除限制
For security, PHP defines a 'basedir', below which you are not allowed to access. As Aleks G says, there is also the file permissions to consider.
This question talks about how to get around basedir restrictions: How can I relax PHP's open_basedir restriction?
Tom Haigh's answer copied to here:
You can also do this easily on a per-directory basis using the Apache (assuming this is your web server) configuration file (e.g. httpd.conf)
you can also completely remove the restriction with
我不是 PHP 程序员,但我认为你的问题是,当 PHP 尝试 opendir 时,它使用与 apache 相同的用户 ID 运行,这可能没有读取你的 /home 目录的权限。
您可以通过更改 /home 的权限或将 apache 用户 ID 添加到拥有 home 组所有权的任何组来解决此问题。
可能是您的 Apache 配置文件有问题。即使文件系统权限允许,您的 httpd.conf 文件也可能不允许访问 /home。如果所有 HTML 文件都位于 /var/www 中,通常会出现这种情况。
httpd.conf 可能被设置为允许从用户的主目录中提供文件。在这种情况下,将授予 /home 内的目录权限,但不会授予 /home 本身。
I'm not a PHP programmer, but I think your problem is that when PHP tries the opendir, it is running with the same user ID as apache has, which may not have permission to read your /home directory.
You could fix that by altering the permissions on /home, or adding the apache userid to whatever group has group ownership of home.
Possibly it is a problem in your Apache configuration file. Even if the filesystem permissions would permit it, your httpd.conf file may not allow access to /home. That would usually be the case if all of your HTML files are in /var/www.
The httpd.conf might be set up to allow serving files out of your users' home directories. In that case the permission would be granted for directories within /home but not for /home itself.
答案就在你的问题中:这是一个权限问题。最有可能的是,运行 Apache 的进程没有读取
/home
目录(或您的用户名,如果在 CLI 中运行)的权限。在终端中手动执行此操作:并检查属性。
The answer is in your question: it's a permission problem. Most likely, the process under which Apache is running does not have permission to read
/home
directory (or your usernamd, if running in CLI). Manually do this in a terminal:and check the attributes.