如何从file_get_contents下载文件? [英] how to download a file from file_get_contents?

查看:146
本文介绍了如何从file_get_contents下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过file_get_contents获取一个文件来复制和重命名它,然后触发下载。换句话说,用户点击指向控制器的链接,控制器进行业务,然后返回新文件进行下载。

I want to get a file via file_get_contents to copy and rename it and then trigger the download. In other words, a user click a link to a controller, the controller does the business and then return the new file to download.

一切都好,我唯一可以直到现在都是重命名并强制执行文件。

All fine, the only thing I can't do till now is rename and force donwload of the file.

推荐答案

您可以更改标题并回显文件:

You can change the headers and echo out the file:

// Download the file
header('Content-Disposition: attachment; filename="myfile.csv"');
header("Content-Type: text/csv");
header("Content-Length: " . filesize($outputName));
echo (file_get_contents($outputName));
unlink($outputName);

这篇关于如何从file_get_contents下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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