scanning /home/ with opendir()

血红的双手。 提交于 2019-12-02 02:12:45

问题


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>

回答1:


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)




回答2:


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>



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/7188806/scanning-home-with-opendir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!