大家好,小瑞又和大家见面了。
今天,给大家介绍的是静态缓存。
首先,看代码
<?php
//var_dump(file_exists('list.html'));//
if(!file_exists('list.html')|| time()-filemtime('list.html')>(60*60*24)){//判断文件是否存在和文件是否过期
echo"no";//输出no 那就没有这个文件
//开启
ob_start();
echo "abc"; //你要输入的内容
//读取里面的内容
$file=ob_get_contents();
//录入
$du=fopen('list.html',"w") or die("Unable to open file!");//用三个函数 开始 录入到页面中 打开 读取 关闭
fwrite($du,$file);
fclose($du);
}else{
echo"yes";//输处yes 直接读取
//读取
echo file_get_contents('list.html');
}
1,首先我们要判断是否有缓存 判断文件是否过期
函数 file_exists("list.html"); 判断是否过期 函数 filemtime;
2,如果 没有 那么我们就要开启 读取 录入函数
开启 ob_start();
读取 ob_get_contents();
录入 fwrite fopen fclose
三个函数一起用
3,如果 有的话 那么就直接读取函数 file_get_contents();
注意:
如果把代码传进 linux中 会报一个
Unable to open file!
这个错误 意思就是权限不够 这里面涉及到二进制 我们需要把权限开到最大
可读 (r 4)可写 (w 2)可执行(x 1)
把权限开到7 就行了 命令行如下
chmod -R 777(文件所在位置)//递归把文件都开到最大的权限 (温馨提示 有风险)
有 -r 的时候是递归,没有 -r 的时候只修改当前文件的权限