1

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.

Toniq
  • 4,492
  • 12
  • 50
  • 109

2 Answers2

1

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

Community
  • 1
  • 1
Sanchit Gupta
  • 3,148
  • 2
  • 28
  • 36
  • if I use download like that, than it just downloads download_init.php file instead of 01.zip – Toniq Apr 18 '17 at 13:50
  • No.... if you are using another file to download your zip file than you have to use proper headers as mentioned by the @poiz below. If you want to use use download attribute directly with anchor tag than you have to link the actual path of zip file as href. – Sanchit Gupta Apr 18 '17 at 13:54
  • my main question was actually is it possible to prevent url change using my method. – Toniq Apr 18 '17 at 14:58
  • As I answered above... adding the download attribute to anchor tag prevent the url redirection. Just pass the zip file link as href.... – Sanchit Gupta Apr 18 '17 at 15:01
1

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....

Poiz
  • 7,611
  • 2
  • 15
  • 17