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.

4
  • Provide download attribute to anchor Commented Apr 18, 2017 at 13:05
  • i think you can use header() function to download file in download_init. Commented Apr 18, 2017 at 13:06
  • 3
    That isn't safe to use at all; use a header and hide the file outside the root for better security. Commented Apr 18, 2017 at 13:08
  • answer is here .refer perishablepress.com/http-headers-file-downloads Commented Apr 18, 2017 at 13:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

if I use download like that, than it just downloads download_init.php file instead of 01.zip
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.
my main question was actually is it possible to prevent url change using my method.
As I answered above... adding the download attribute to anchor tag prevent the url redirection. Just pass the zip file link as href....
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....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.