how to download a file from file_get_contents?

不羁岁月 提交于 2020-01-14 06:49:46

问题


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.


回答1:


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);


来源:https://stackoverflow.com/questions/25082482/how-to-download-a-file-from-file-get-contents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!