how to download a file from file_get_contents?

2019-06-11 05:04发布

问题:

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