Reputation: 4996
I am using this to download file:
<a href="download_init.php?Down=01.zip">download here</a>
download_init.php:
<?php
$Down=$_GET['Down'];
?>
<html>
<head>
<meta http-equiv="refresh" content="0;url=<?php echo $Down; ?>">
</head>
<body>
</body>
</html>
Is there a way to prevent browser url changing when clicking on the link without ajax?
download_init.php?Down=01.zip
Check here: http://www.firegrid.co.uk/scripts/download/index.php
Click on the first link, the url doesnt change, unlike other links.
Upvotes: 1
Views: 721
Reputation: 7617
You could perform an actual Download using a handful of header
Functions as depicted in the Code below.
However; first, you may need to create an Arbitrary Processing File (example: download_init.php
) at the Root of your App. Now inside that download_init.php
File, you could add something like this:
<?php
// CHECK THAT THE `d` PARAMETER IS SET IN THE GET SUPER-GLOBAL:
// THIS PARAMETER HOLDS THE PATH TO THE DOWNLOAD-FILE...
// IF IT IS SET, PROCESS THE DOWNLOAD AND EXIT...
if(isset($_GET['d']) && $_GET['d']){
processDownload($_GET['d']);
exit;
}
function processDownload($pathToDownloadFile, $newName=null) {
$type = pathinfo($pathToDownloadFile,
PATHINFO_EXTENSION);
if($pathToDownloadFile){
if(file_exists($pathToDownloadFile)){
$size = @filesize($pathToDownloadFile);
$newName = ($newName) ? $newName . ".{$type}" :basename($pathToDownloadFile);
header('Content-Description: File Transfer');
header('Content-Type: ' . mime_content_type ($pathToDownloadFile ));
header('Content-Disposition: attachment; filename=' . $newName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);
return(readfile($pathToDownloadFile));
}
}
return FALSE;
}
However, this implies that your Links would now have slightly different href
values like so:
<!-- THIS WOULD TRIGGER THE DOWNLOAD ONCE CLICKED -->
<a href="download_init.php?d=path_to_01.zip">download here</a>
If you find this Header approach too extraneous for your purpose; @Sanchit Gupta provided a Solution with the HTML5 download
Attribute....
Upvotes: 1
Reputation: 3224
Add a download
attribute to anchor tag :
<a href="download_init.php?Down=01.zip" download>download here</a>
For more detail on HTML download Attribute
If you want to use header
please refer this link
Upvotes: 1