编写文本文件并强制使用php下载 [英] Write a text file and force to download with php

查看:62
本文介绍了编写文本文件并强制使用php下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Im using a button to direct to a script which creates a text file:

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

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

after that I want the web browser to propose to download or open this file how should I do? Thanks

推荐答案

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

这篇关于编写文本文件并强制使用php下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆