24

我使用按钮指向创建文本文件的脚本:

<?php
$handle = fopen("file.txt", "w");
fwrite($handle, "text1.....");
fclose($handle);
?>

之后我希望网络浏览器建议下载或打开这个文件我应该怎么做?谢谢

4

2 回答 2

51

使用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;
?>
于 2012-08-10T14:15:14.633 回答
6
$content = file_get_contents ($filename);
header ('Content-Type: application/octet-stream');
echo $content;

应该管用

于 2012-08-10T14:16:09.777 回答