使用 opendir() 扫描 /home/

发布于 2024-12-01 11:49:29 字数 207 浏览 1 评论 0原文

是否可以使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千笙结 2024-12-08 11:49:29

您可以使用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, probably 0755 (rwxr-xr-x)

手长情犹 2024-12-08 11:49:29

为了安全起见,PHP 定义了一个“basedir”,低于该目录的用户不允许访问。正如 Aleks G 所说,还需要考虑文件权限。

这个问题讨论了如何绕过basedir限制: How can I Relax PHP's open_basedir 限制?

Tom Haigh 的答案复制到此处:

您还可以使用 Apache(假设这是您的 Web 服务器)配置文件(例如 httpd.conf)在每个目录上轻松完成此操作,

<Directory /var/www/vhosts/domain.tld/httpdocs>
php_admin_value open_basedir "/var/www/vhosts/domain.tld/httpdocs:/var/www/vhosts/domain.tld/zend"
</Directory>

您也可以完全消除限制

<Directory /var/www/vhosts/domain.tld/httpdocs>
php_admin_value open_basedir none
</Directory>

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)

<Directory /var/www/vhosts/domain.tld/httpdocs>
php_admin_value open_basedir "/var/www/vhosts/domain.tld/httpdocs:/var/www/vhosts/domain.tld/zend"
</Directory>

you can also completely remove the restriction with

<Directory /var/www/vhosts/domain.tld/httpdocs>
php_admin_value open_basedir none
</Directory>
浪菊怪哟 2024-12-08 11:49:29

我不是 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.

青瓷清茶倾城歌 2024-12-08 11:49:29

答案就在你的问题中:这是一个权限问题。最有可能的是,运行 Apache 的进程没有读取 /home 目录(或您的用户名,如果在 CLI 中运行)的权限。在终端中手动执行此操作:

ls -ld /home

并检查属性。

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:

ls -ld /home

and check the attributes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文