首頁  >  文章  >  後端開發  >  centos7 - 奇怪的php檔案讀寫權限問題【全777+apache:apache】?

centos7 - 奇怪的php檔案讀寫權限問題【全777+apache:apache】?

WBOY
WBOY原創
2016-12-01 00:56:432411瀏覽

如題,我目前的網站目錄是/var/www/html(777權限)
然後我的
/var/www/html/g/data/visit.php 檔案

<code>require("../function.php");
if (file_exists($_SERVER["DOCUMENT_ROOT"]. "/cache/vc-$date.json")) {
    echo file_get_contents($_SERVER["DOCUMENT_ROOT"]. "/cache/vc-$date.json");
} else {
    $res=get_sum();
    $days1=get_days(1);
    $month=get_this_month();

    $result=["visit"=>$res,'days'=>$days1,'month'=>$month];
    $echo = json_encode($result);
    cache_this($echo,'vc-'.$date);
    echo $echo;
}

</code>

function.php

<code>function cache_this($result,$name){//内容,命名规则
  //file_put_contents("/cache/$name.json", $result)or die("Unable to open file!");

    $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/cache/$name.json", "w") or die("Unable to open file!");
    $txt = $result;
    fwrite($myfile, $txt);
    fclose($myfile);
}</code>

這是我ls -l的部分結果

<code>-rwxrwxrwx.  1 root   root        81 11月  4 2013 bottom.php
drwxrwxrwx.  2 apache apache    4096 11月 10 17:09 cache</code>

然後我看了httpd.conf裡面的user和group,都是apache沒有錯
但是這個json檔案就是寫不進cache資料夾裡面,$myfile總是回傳false。
求教這個是什麼問題,已經折騰了一晚上,雙十一centos討論群裡面都在水,也沒問出個究竟……
急求…………………………(無力

回覆內容:

如題,我目前的網站目錄是/var/www/html(777權限)
然後我的
/var/www/html/g/data/visit.php 檔案

<code>require("../function.php");
if (file_exists($_SERVER["DOCUMENT_ROOT"]. "/cache/vc-$date.json")) {
    echo file_get_contents($_SERVER["DOCUMENT_ROOT"]. "/cache/vc-$date.json");
} else {
    $res=get_sum();
    $days1=get_days(1);
    $month=get_this_month();

    $result=["visit"=>$res,'days'=>$days1,'month'=>$month];
    $echo = json_encode($result);
    cache_this($echo,'vc-'.$date);
    echo $echo;
}

</code>

function.php

<code>function cache_this($result,$name){//内容,命名规则
  //file_put_contents("/cache/$name.json", $result)or die("Unable to open file!");

    $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/cache/$name.json", "w") or die("Unable to open file!");
    $txt = $result;
    fwrite($myfile, $txt);
    fclose($myfile);
}</code>

這是我ls -l的部分結果

<code>-rwxrwxrwx.  1 root   root        81 11月  4 2013 bottom.php
drwxrwxrwx.  2 apache apache    4096 11月 10 17:09 cache</code>

然後我看了httpd.conf裡面的user和group,都是apache沒有錯
但是這個json檔案就是寫不進cache資料夾裡面,$myfile總是回傳false。
求教這個是什麼問題,已經折騰了一晚上,雙十一centos討論群裡面都在水,也沒問出個究竟……
急求…………………………(無力

查看SELinux狀態

為啥非要寫到根目錄 目前目錄不行麼

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn