【问题标题】:fopen creates files owned by root in phpfopen 在 php 中创建 root 拥有的文件
【发布时间】:2018-12-02 00:42:52
【问题描述】:

我正在编写将 disqus cmets 转换为 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

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

编辑:

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

【问题讨论】:

  • 你是在浏览器中运行它还是使用 CLI
  • @RiggsFolly 来自浏览器。
  • @DaFois 这不是重复的,因为它没有更改权限,而是使其不会更改为root:root,但像其他文件一样保留apache:apache,并且在查看其他问题时似乎对于 chown,您将需要修改 sudoerrs,这对我来说不是一个选项。
  • 看起来您的服务器配置错误,而 Apache 进程实际上是以 root 身份运行的。

标签: php file-permissions


【解决方案1】:

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

我用过这个:

chmod($dir, 0775);

代替:

chmod($dir, 0774);

该目录对其他人是不可执行的,并且它在该目录中创建了由 root 拥有的文件。很奇怪。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2020-12-05
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多