I am using header in PHP in order to force downloading a text file. This is the code:
$file = fopen("download/file.txt",'w');
fwrite($file,"some text" . PHP_EOL);
fclose($file);
header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
header("Content-Type: text/plain");
header("Content-Length: " . filesize($file));
header("Connection: close");
When I click the button, file is generated correctly, and I can open it in a text editor, but doesn't download because this: "Error: incomplete file". Where am I wrong?
Thanks for the comments. You are right, I had to add readfile()
to my code. With this line at the end of the code it works good:
readfile($file);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.