简体   繁体   English

无法使用PHP强制下载文件

[英]Unable to force download file with PHP

I need to create a PHP page which starts automatically the download a file (I don't want to expose the Download link). 我需要创建一个PHP页面,该页面会自动开始下载文件(我不想公开Download链接)。 I've tried with several examples on the web but all examples will end up with opening the file in the browser with the uncorrect content type. 我已经在网络上尝试了几个示例,但是所有示例最终都将在浏览器中使用不正确的内容类型打开文件。 For example: 例如:

 <?php
// We'll be outputting a ZIP
header("Content-type: application/zip");

// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header("Content-Disposition: attachment; filename=downloaded.zip");

readfile('downloaded.zip');
?>

When I execute this page, the output on the browser is: 当我执行此页面时,浏览器上的输出是: 在此处输入图片说明 After trying all possible variants of this example my idea is that the problem is with my hosting environment. 在尝试了本示例的所有可能变体之后,我的想法是问题出在我的托管环境上。 Which variable should I check and maybe ask to be enabled to my provider ? 我应该检查哪个变量,或者可能要求对提供程序启用它?
Thanks! 谢谢!

Try printing out the following variable using phpinfo(): SERVER["HTTP_ACCEPT_ENCODING"]. 尝试使用phpinfo()打印以下变量:SERVER [“ HTTP_ACCEPT_ENCODING”]。 My suspect is that you don't have zip allowed- could be something different like "gzip" for example. 我的怀疑是您不允许使用zip,例如,可能与“ gzip”不同。

Here's the code I used back in the day: 这是我以前使用的代码:

                        header ("Content-Type: application");
                        header ("Content-Disposition: attachment; filename=\"$file\"");
                        header ("Content-Length: " . filesize ('./FILES/' . $file));
                        header ("Cache-Control: no-cache");
                        header ("Pragma: no-cache");
                        readfile ('./FILES/' . $file);
Just modify the content-type header ofc. 只需修改内容类型标头ofc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM