写一个文本文件,并强制下载与php

nnsrf1az  于 2023-08-02  发布在  PHP
关注(0)|答案(2)|浏览(102)

我正在使用一个按钮来指向一个创建文本文件的脚本:

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

字符串
之后,我想Web浏览器建议下载或打开此文件,我该怎么办?谢啦,谢啦

3bygqnnd

3bygqnnd1#

使用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;
?>

字符串

lskq00tm

lskq00tm2#

$content = file_get_contents ($filename);
header ('Content-Type: application/octet-stream');
echo $content;

字符串
应该可以

相关问题