fopen 在 php 中创建 root 拥有的文件

fopen creates files owned by root in php

我正在编写将 disqus 评论转换为 HashOver 系统的代码,我有这样的代码:

// $threads is list of threads from disqus api that I cached on disk
foreach ($threads as $thread) {
    $dir_name = getSafeThreadName(preg_replace("%^https?://[^/]+%", "", $thread['link']));
    $dir = 'threads/' . $dir_name;
    if (!is_dir($dir)) {
        mkdir($dir);
        chmod($dir, 0774);
    }
    // ...
    // $thread_posts are $post that belong to $thread
    foreach($thread_posts as $post) {
        $xml = new SimpleXMLElement('<comment/>');
        // ...
        // $name_arr is array of numbers for HashOver comments
        $fname = $dir . '/' . implode('-', $name_arr) . ".xml";
        $f = fopen($fname, 'w');
        fwrite($f, $xml->asXML());
        fclose($f);
        chmod($fname, 0664);
    }
}

它为我在线程中的每个帖子创建了目录,即 read/write 所有者 apache:apache 并且里面有像 1.xml 所有者 root:root

为什么 root:root?我怎样才能做到 apache:apache?

编辑:

这不是重复的,我不想将权限从 root:root 更改为 apache:apache,这可以使用 chown 完成,但我想这样做本来就不改成root:root,我也想知道为什么改成root:root。对于我来说,这看起来像是 php 或 apache 中的错误,或者我这边的 apache 中的一些错误配置。我不认为是代码,因为它只是打开、写入和关闭文件。

不知道为什么,我想知道,但这解决了问题:

我用过这个:

chmod($dir, 0775);

而不是:

chmod($dir, 0774);

目录对其他人不可执行,它在创建时在 root 拥有的目录中生成文件。很奇怪。