Home  >  Article  >  Backend Development  >  centos7 - Strange php file read and write permission problem [all 777+apache:apache]?

centos7 - Strange php file read and write permission problem [all 777+apache:apache]?

WBOY
WBOYOriginal
2016-12-01 00:56:432324browse

As mentioned, my current website directory is /var/www/html (777 permissions)
Then my
/var/www/html/g/data/visit.php file

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;
}

function.php

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

This is part of the result of my ls -l

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

Then I looked at the user and group in httpd.conf, and they were all correct for apache.
But the json file just couldn’t be written into the cache folder, and $myfile always returned false.
Please tell me what the problem is. I have been struggling all night. The Double Eleven centos discussion group is full of troubles and I can’t find out what is going on...
Urgent request……………… (powerless

Reply content:

As mentioned, my current website directory is /var/www/html (777 permissions)
Then my
/var/www/html/g/data/visit.php file

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;
}

function.php

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

This is part of the result of my ls -l

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

Then I looked at the user and group in httpd.conf, and they were all correct for apache.
But the json file just couldn’t be written into the cache folder, and $myfile always returned false.
Please tell me what the problem is. I have been struggling all night. The Double Eleven centos discussion group is full of troubles and I can’t find out what is going on...
Urgent request……………… (powerless

View SELinux status

Why do you have to write to the root directory? Can’t the current directory work?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn