编写文本文件并强制使用php下载
2022-08-30 17:34:20
Im使用按钮定向到创建文本文件的脚本:
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
?>
之后,我希望Web浏览器建议下载或打开此文件,我该怎么办?谢谢
Im使用按钮定向到创建文本文件的脚本:
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
?>
之后,我希望Web浏览器建议下载或打开此文件,我该怎么办?谢谢
使用 readfile() 和标头application/octet-stream
<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename('file.txt'));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('file.txt'));
readfile('file.txt');
exit;
?>
$content = file_get_contents ($filename);
header ('Content-Type: application/octet-stream');
echo $content;
应该工作