Create a file and download 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:

$content =  "All content here";

   if( empty( $error )){ 

   echo "

File generated

"; $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 "" . "Download" ;

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

Solution 2:

i think it’s a ‘cache problem’. try this:

echo "Download";

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

Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » Create a file and download php

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录