dty3416
2017-10-23 13:37
浏览 590
已采纳

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.

图片转代码服务由CSDN问答提供 功能建议

我试图使用

 <从JSON文件中读取常量 code> $ const = file_get_contents('/ myfolder / const.json'); 
   
 
 

问题是这将始终从缓存中读取文件,而不是从本地XAMPP读取 服务器。 在开发阶段,由于编辑,此文件是动态的,每当文件发生更改时,我最终都会清除浏览器缓存。 我怎么能强制总是从服务器读取(在开发阶段这样做的延迟很好。我将切换到正常的部署后 - 如何?)? 任何帮助表示赞赏。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • douxia6163 2017-10-23 13: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条)

相关推荐 更多相似问题