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

WBOY
Release: 2023-03-02 06:12:01
Original
2458 people have browsed it

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

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

This is part of the result of my ls -l

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

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

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

This is part of the result of my ls -l

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

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?

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template