用PHP强制下载页面 [英] Forcing page download in PHP

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

问题描述

如何强制浏览器下载浏览到的当前页面?标题为Content-type:text/plain的页面,例如使用PHP?

How do I force the browser to download the current page browsed to? A page with the header Content-type: text/plain for example using PHP?

如果用户导航到该页面,则会出现一个下载框(浏览器的下载对话框,通常带有另存为".

If a user navigates to that page, a download box should appear (the browser download dialog with the usual "Save As".

推荐答案

直接来自 http://php.net/header

<?php
    // There is contention over if this MIME type is right, but just use it for now.
    header('Content-type: text/javascript');

    header('Content-Disposition: attachment; filename="file.js"');

    readfile('file.js'); // Echo the file
?>

注意:此操作必须在任何其他输出之前完成(并且可能是页面上唯一的事情,除非您希望文件中有其他输出).

NOTE: this must be done before any other output (and can be about the only thing on the page, unless you want other output in your file).

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

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