在 php 中更改目录权限 (chmod)
我已经用谷歌搜索过,但我似乎无法让我的脚本工作。
这是我的代码,
if (is_dir("tmp")) {
if (substr(sprintf("%o", fileperms("tmp")), -4) == "0777") {
echo "good";
} else {
echo "going to chmod the tmp folder to 777";
if (!chmod("tmp", octdec(0777))) { // tried chmod("tmp, 0777) too
echo "Oops, I couldn't chmod the /setup/tmp directory, please do this manually";
}
}
} else {
echo "we'll make the folder";
}
我似乎无法让 chmod 工作,我在某处读到,如果寄存器全局变量关闭,那么这将不起作用(我已将该设置设置为关闭)。
我遵循了 PHP.net 手册和他们在用户评论中提供的一些示例,以及我发现相关的一些堆栈溢出帖子。但也许我需要调整一些 php 设置才能使其正常工作?
有没有办法让 chmod 函数工作而不需要更改 PHP.ini?
I have googled but I can't seem to get my script to work.
this is my code
if (is_dir("tmp")) {
if (substr(sprintf("%o", fileperms("tmp")), -4) == "0777") {
echo "good";
} else {
echo "going to chmod the tmp folder to 777";
if (!chmod("tmp", octdec(0777))) { // tried chmod("tmp, 0777) too
echo "Oops, I couldn't chmod the /setup/tmp directory, please do this manually";
}
}
} else {
echo "we'll make the folder";
}
I can't seem to get the chmod to work, i read somewhere that if register globals was off then this wouldn't work(i have that setting set to off).
I followed the PHP.net manual and some of the examples they provided there in the user comments, as well as some stack-overflow posts that i found relevant. but perhaps i need to tweak some php settings to get this to work?
Is there a way to get the chmod function to work without needing to change the PHP.ini?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有向我们提供足够的信息来确定为什么
chmod()
失败了。可能是 Web 服务器没有足够的权限来更改目录权限。Web 服务器进程的所有者应该拥有该目录或者是所属组的成员。例如,如果 root 拥有 tmp,并且您的网络服务器是以用户“apache”运行,将无法从 PHP 修改目录的权限。
我相信您在 php.ini 中提到的设置是用于调试,而不是神奇地“修复”
chmod
。尝试将以下代码行添加到 PHP 脚本中,您应该会获得更有用的调试输出:You haven't given us enough information to determine why
chmod()
is failing. It could be that the web-server doesn't have sufficient privileges to alter directory permissions.The owner of the web-server process should own the directory or be a member of the owning group. For example, if root owns tmp, and your web-server is running as the user 'apache', it will not be possible to modify the directory's permissions from PHP.
I believe the settings you mention in php.ini are for debugging, not magically 'fixing'
chmod
. Try prepending the following lines of code to your PHP script and you should get more useful debug output:表示如下:-
Represent like this:-
运行 php 守护程序或 Web 服务器守护程序的用户必须对您尝试 chmod 的目录具有写入权限。就我而言,www-data 是运行 php 的用户,php 尝试运行的任何命令都将以 www-data 用户身份运行。
如果您使用 php mkdir 函数创建一个目录,您会注意到该目录的所有者是运行 php 或 Web 服务器守护程序的人。任何由 php 创建的目录,php 将能够 chmod、chown 对其执行任何操作,因为它是所有者。
您可以
The user that is running the php daemon or web server daemon must have write access to the directory you are trying to chmod. In my case, www-data is the user that is running php and any commands that php is trying to run, it will be run as the www-data user.
If you create a directory using the php mkdir function, you will notice that the owner of the directory is who ever is running the php or webserver daemon. And any directory that was created by php, php will be able to chmod, chown, do anything to it because it is the owner.
You can either