dty3416 2017-10-23 05:37
浏览 615
已采纳

PHP:file_get_contents()强制从服务器而不是缓存中读取文件

I am trying to read my constants from a JSON file using

$const = file_get_contents('/myfolder/const.json');

Problem is this will always read the file from cache and not from my local XAMPP server. During development phase, this file is dynamic due to edits and I end up clearing the browser cache everytime there's a change in the file. How can I force to read from the server always (latency in doing this during development phase is fine. I will switch to normal post deployment - how?)? Any help is appreciated.

  • 写回答

3条回答 默认 最新

  • douxia6163 2017-10-23 05:42
    关注

    There are a couple of "accepted" solutions.

    The first is to add a timestamp to the file like

    $const = file_get_contents('/myfolder/const.json?'.date("Ymdhis"));
    

    You can also try this

    header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    

    I suggest adding one of these, clearing your cache and then try to run the code. Adding the timestamp to the file has worked for me, when I add it before I start devving, and reloading the page.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部