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,
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>
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:
function download(path,val) {
window.location.href = path+"download.php?val="+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 {