在 php 中更改目录权限 (chmod)

发布于 2024-12-14 21:53:16 字数 669 浏览 0 评论 0原文

我已经用谷歌搜索过,但我似乎无法让我的脚本工作。

这是我的代码,

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 技术交流群。

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

发布评论

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

评论(3

帅的被狗咬 2024-12-21 21:53:16

您没有向我们提供足够的信息来确定为什么 chmod() 失败了。可能是 Web 服务器没有足够的权限来更改目录权限。

Web 服务器进程的所有者应该拥有该目录或者是所属组的成员。例如,如果 root 拥有 tmp,并且您的网络服务器是以用户“apache”运行,将无法从 PHP 修改目录的权限。

我相信您在 php.ini 中提到的设置是用于调试,而不是神奇地“修复”chmod。尝试将以下代码行添加到 PHP 脚本中,您应该会获得更有用的调试输出:

ini_set('display_errors', 'On');
error_reporting(E_ALL)

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:

ini_set('display_errors', 'On');
error_reporting(E_ALL)
绝影如岚 2024-12-21 21:53:16

表示如下:-

chmod("tmp", 0777);

Represent like this:-

chmod("tmp", 0777);
妄司 2024-12-21 21:53:16

运行 php 守护程序或 Web 服务器守护程序的用户必须对您尝试 chmod 的目录具有写入权限。就我而言,www-data 是运行 php 的用户,php 尝试运行的任何命令都将以 www-data 用户身份运行。

如果您使用 php mkdir 函数创建一个目录,您会注意到该目录的所有者是运行 php 或 Web 服务器守护程序的人。任何由 php 创建的目录,php 将能够 chmod、chown 对其执行任何操作,因为它是所有者。

您可以

  1. chown 目录,以便所有者是 php 用户,
  2. 为组设置写入权限,并将组设置为与 php 用户相同的组,
  3. 执行诸如向 php 用户授予 sudo 权限之类的操作,顺便说一句,不建议这样做。

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

  1. chown the directory so that the owner is the php user
  2. set writing privileges for the group and set the group to the same group as the php user
  3. do something like giving sudo privileges to the php user which is not recommended btw.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文