dongtao6842 2018-12-01 16:01
浏览 135
已采纳

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

I'm writing the code for converting disqus comments to HashOver system, and I have code like this:

// $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);
    }
}

it created directory for each of my posts in threads that that is read/write with owner apache:apache and inside there are files like 1.xml with owner root:root

why root:root? How can I make it apache:apache?

EDIT:

This is no a duplicate, I don't want to change permission to apache:apache from root:root, which can be done using chown, but I want to make it so it don't change it to root:root in first place, I also want to know why it changed to root:root. This looks like a bug in php or apache for me or some wrong configuration in apache on my side. I don't think is the code since it just open, write and close file.

  • 写回答

1条回答 默认 最新

  • douwei1904 2018-12-01 16:42
    关注

    Don't know why and I would like to know, but this solve the issue:

    I've use this:

    chmod($dir, 0775);
    

    instead of:

    chmod($dir, 0774);
    

    directory was not executable for others and it make files in that directory owned by root when created. Very weird.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记