php:将变量内容下载为文件 [英] php: download variable content as file

查看:159
本文介绍了php:将变量内容下载为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题是否可能?我有一个脚本执行。有一点我有一大块文本在变量中。可以将其作为可下载文件提供,而不会将可变内容写入磁盘?

 <?php 
echo 你好;
//如何使此变量的内容可下载?
$ download_me =下载我...;
echoBye;
?>


解决方案

如果您的意思是让用户点击链接,弹出一个对话框,将某些内容保存为文本文件:

 <?php 
$ download_me =下载我...;
header(Content-type:text / plain);
header(Content-Disposition:attachment; filename = test.txt);
echo $ download_me;
?>

这是你的目标?另外,您可能想要写一些行,只允许以这种方式发送标题,如果某个 $ _ POST $ _ GET 变量设置。


Is the subject possible? I have a script executing. At one point I have a large piece of text in a variable. Could I make it available as a downloadable file without actually writing variable content to disk?

<?php
    echo "Hello";
    //how do I make the content of this variable downloadable?
    $download_me = "download me...";
    echo "Bye";
?>

解决方案

If you mean letting the user click a link and have a dialog box pop up to save certain content as a text file:

<?php
$download_me = "download me...";
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=test.txt");
echo $download_me;
?>

Is that what you're aiming at? Also, you might want to write a few lines that only allows the headers to be sent this way if a certain $_POST or $_GET variable is set.

这篇关于php:将变量内容下载为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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