如何下载一个文件上单击文件使用PHP的名字吗? [英] How to download a file on clicking the name of file using PHP?
本文介绍了如何下载一个文件上单击文件使用PHP的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个信息表,其中有在那里的文件名被超链接的字段。我希望用户下载特定文件,当他点击就可以了。
那么,如何下载文件上单击文件的名称使用PHP?
我试图用ajax的code是如下,但是我没有得到任何文件下载。
的download.php
$文件名= $ _GET [VAL'];
//获取文件信息。
$文件路径= $ _ SERVER ['DOCUMENT_ROOT']。 /dfms/images/docs/".$filename;
如果(file_exists($文件路径)){
$文件名=基名($文件路径);
$档案大小=档案大小($文件路径);
//输出头。
标题(缓存控制:私人);
标题(内容类型:应用程序/流);
标题(内容长度:$文件大小。);
标题(内容处置:附件;文件名=$文件名);
// 输出文件。
ReadFile的($文件路径);
出口();
}
其他 {
死亡(提供的文件路径无效。);
}
JavaScript函数
<脚本>
传播Inint_AJAX(){
尝试{返回新的ActiveXObject(MSXML2.XMLHTTP); }赶上(五){} // IE
尝试{返回新的ActiveXObject(Microsoft.XMLHTTP); }赶上(五){} // IE
尝试{返回新XMLHtt prequest(); }赶上(五){} //本地JavaScript
警报(不支持XMLHtt prequest);
返回null;
};
功能下载(路径,VAL){
VAR REQ = Inint_AJAX();
(?的download.php VAL =GET,路径+ + VAL)req.open; //使连接
//req.setRequestHeader("Content-Type,应用程序/ x-WWW的形式urlen codeD;字符集= ISO-8859-1); //设置页眉
req.send(空); //发送值
req.onreadystatechange =功能(){
如果(req.readyState == 4){
如果(req.status == 200){
}
}
};
}
< / SCRIPT>
链接下载
< A HREF =JavaScript的:下载('HTTP://localhost/project/images/','DMS.doc');> DMS.doc< / a取代;
解决方案
那么,如何下载文件上单击文件的名称使用PHP?
最简单的方法应该链接到它。
< A HREF =?的download.php ......TARGET =_空白> DMS.doc< / A>
I Have a list of information in which there is a field where name of file is hyperlinked. I want the user to download that particular file when he clicks on it.
so, How to download a file on clicking the name of file using PHP?
I tried it using ajax whose code is as below, but I do not get any file downloaded.
download.php
$filename = $_GET['val'];
// Fetch the file info.
$filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;
if(file_exists($filePath)) {
$fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/stream");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
javascript function
<script>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function download(path,val) {
var req = Inint_AJAX();
req.open("GET", path+"download.php?val="+val); //make connection
//req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
}
}
};
}
</script>
link to download
<a href="javascript:download('http://localhost/project/images/','DMS.doc');">DMS.doc</a>
解决方案
so, How to download a file on clicking the name of file using PHP?
The easiest way should be linking to it.
<a href="download.php?......" target="_blank">DMS.doc</a>
这篇关于如何下载一个文件上单击文件使用PHP的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文