开发者

Download file created by PHP without changing windows

开发者 https://www.devze.com 2023-03-15 20:54 出处:网络
I\'m generating a file whenever a php script is run, which is downloaded in at the same time by the user (no copies are saved to the server).

I'm generating a file whenever a php script is run, which is downloaded in at the same time by the user (no copies are saved to the server).

Currently I'm redirecting a user from the file creation php script back to the original page.

I'd like to not redirect the user back to the original page but have them stay on the ori开发者_如何学编程ginal page and the PHP script to still run and prompting them to save the custom created file.

I'm not entirely sure, but would be adding/changing something from the end of my current PHP script or simply changing my HTML link to it somehow? I'm

My HTML

<a href="fileCreator.php">Download File</a>

My PHP

header("Content-type: text/txt");
header("Content-Disposition: attachment; filename=file.txt");
header("Pragma: no-cache");
header("Expires: 0");
header("Location: [ORIGINAL PAGE]");
echo $txtFile; 


If you set a target on your link to an iframe (which you can set to be hidden), you can have the download prompt come up and your user will never have to leave the page on which they clicked the download link.

<a href="fileCreator.php" target="download_frame">Download File</a>
<iframe id="download_frame" style="display:none;"></iframe>


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: $fileType");
header("Content-Length: $fileSize");
header("Content-Transfer-Encoding: binary");
echo $fileContent;

Then just link to this PHP file, it shouldn't make the user leave the page.


Try adding

header('Content-Transfer-Encoding: binary');
header("Content-type: application/force-download"); 
header('Content-Type: application/octet-stream'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消