代码之家  ›  专栏  ›  技术社区  ›  jcubic

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

  •  0
  • jcubic  · 技术社区  · 7 年前

    我正在编写将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);
        }
    }
    

    它为我在线程中的每个文章创建了一个目录,这些线程是用所有者读/写的。 apache:apache 里面有文件 1.xml 与业主 root:root

    为什么 根:根 ?我怎么做 Apache:Apache 是吗?

    编辑:

    这不是副本,我不想将权限更改为 Apache:Apache 根:根 ,可以使用 chown 但是我想做,这样它就不会变为 根:根 首先,我还想知道为什么它变成了 根:根 .对于我来说,这看起来像是php或apache中的一个bug,或者是apache中的一些错误配置。我不认为是代码,因为它只是打开、写入和关闭文件。

    1 回复  |  直到 7 年前
        1
  •  0
  •   jcubic    7 年前

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

    我用过这个:

    chmod($dir, 0775);
    

    而不是:

    chmod($dir, 0774);
    

    目录对其他人来说不是可执行的,它在创建时使根目录拥有的目录中的文件成为可执行文件。非常奇怪。

    推荐文章