PHP自动下载文件,而不提示您保存或打开文件 [英] PHP automatically downloads file without prompting to save or open file

查看:187
本文介绍了PHP自动下载文件,而不提示您保存或打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能,可以用来从非公共目录下载文件。下载部分工作正常,除了它不会在之前提示用户并且允许用户选择位置或只是简单地打开。

I have a function which I use to enable downloading of files from a non-public directory. The downloading part works fine except that it does not prompt the user before and allowing the user to choose the location or just a simple open.

我使用以下代码:

$file = L_APP_BILAGOR."/".$_GET["f"];
$finfo = new finfo(FILEINFO_MIME);
$ct = $finfo->file($fileName);

if (file_exists($file)) {
   header('Content-Type: '.$ct);
   header('Content-Disposition: attachment; filename='.basename($file));
   header('Content-Transfer-Encoding: binary');
   header('Expires: 0');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Pragma: public');
   header('Content-Length: ' . filesize($file));
   ob_clean();
   flush();
   readfile($file);
   exit;
}

似乎无法正确处理。如何强制浏览器在下载文件之前提示用户?

Can't seem to get it right. How do I force the browser to prompt the user before downloading the file?

推荐答案

我不确定这是否真的是php的东西,我相信它更多的是基于浏览器设置。一般来说,
对我来说还不错,当我比较它时和其他示例

i am not sure if this is really a php thing, i believe it´s more based on the browser settings. in general your code looks okay for me, when i compare it with other examples.

这篇关于PHP自动下载文件,而不提示您保存或打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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