duanchu3376 2014-02-19 23:26
浏览 40
已采纳

创建文件并下载它的PHP

I'm trying to generate a .html file and download it.

My issue: First i check if file exists, if true i delete it and create a new one.

When i click to generate file, the downloaded file is always the first file created and isnt updated. I've checked the file manualy and its ok but the downloaded is the old one. If i delete the file on server i can download anyway.

check the site: bit.ly/1crrcif

$content =  "<html></head></head><body>All content here</body></html>";

   if( empty( $error )){ 

   echo "<h3>File generated</h3>";
   $my_file = 'change_nameto_index.html';
   if (file_exists($my_file)) {
       echo "file exists";
      unlink($my_file);
   }

   $new_file = 'change_nameto_index.html';
   $handle = fopen($new_file, 'w') or die('Cannot open file:  '.$new_file);
   $data = $content;
   fwrite($handle, $data);
   fclose($handle);
   echo "<a download='change_nameto_index.html' href='change_nameto_index.html'>"
      . "<b class='download'>Download</b></a>"
      ;
  • 写回答

2条回答 默认 最新

  • douhuan1380 2014-02-19 23:30
    关注

    solution 1: try to add this to your download page head:

    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    

    Solution 2:

    i think it's a 'cache problem'. try this:

    echo "<a download='index.html?".time()."' href='index.html?".time()."'><b class='download'>Download</b></a>";
    

    this isn't the best way but probably the fast (and easy) one

    adding a parameter that is every time different (with '?' and time()) will make the browser thinking that the download page is also different, so it will download it's latest version

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器