PHP:点击按钮下载文件 [英] PHP: Download a file on button click?

查看:199
本文介绍了PHP:点击按钮下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个表单,当单击一个包含的按钮时,应该打开一个下载对话框来下载某个文件。文件放在同一个服务器上。

I created a form, which, when a contained button is clicked, should open a download dialog to download a certain file. The file is placed on the same server.

我试过:

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=" . $file . '"'); 

其中$ file是本地路径+文件名,例如c:\mypath\myfile.xls。这不行,它提供了一个文件,但它不是一个有效的文件。我可以这样做吗?

Where $file is a local path + the file name, for example c:\mypath\myfile.xls. This does not work though. It offers me a file, but its not a valid file. How else could I do that?

注意:我写了c:\,因为它仍然在我的本地机器上进行测试。

Note: I wrote c:\ because its still on my local machine for testing.

谢谢!

推荐答案

尝试这个

header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
die(file_get_contents($file));

我认为file_get_contents()函数不再适用于PHP 5.0.3

I think file_get_contents() function is no longer work with PHP 5.0.3

这篇关于PHP:点击按钮下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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