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?
那么,如何使用PHP单击文件名下载文件?
I tried it using ajax whose code is as below, but I do not get any file downloaded.
我尝试使用ajax,其代码如下,但我没有下载任何文件。
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>
3 个解决方案
#1
5
so, How to download a file on clicking the name of file using PHP?
那么,如何使用PHP单击文件名下载文件?
The easiest way should be linking to it.
最简单的方法应该是链接到它。
<a href="download.php?......" target="_blank">DMS.doc</a>
#2
4
You don't need AJAX for that, you cannot even do what you want using AJAX. If you want to obfuscate URL to a file, then replace your download function with this:
你不需要AJAX,你甚至无法使用AJAX做你想做的事。如果要将URL混淆为文件,请将下载函数替换为:
function download(path,val) {
window.location.href = path+"download.php?val="+val;
};
#3
-1
// 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 {
#1
5
so, How to download a file on clicking the name of file using PHP?
那么,如何使用PHP单击文件名下载文件?
The easiest way should be linking to it.
最简单的方法应该是链接到它。
<a href="download.php?......" target="_blank">DMS.doc</a>
#2
4
You don't need AJAX for that, you cannot even do what you want using AJAX. If you want to obfuscate URL to a file, then replace your download function with this:
你不需要AJAX,你甚至无法使用AJAX做你想做的事。如果要将URL混淆为文件,请将下载函数替换为:
function download(path,val) {
window.location.href = path+"download.php?val="+val;
};
#3
-1
// 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 {