9

The purpose is to download the dumped backup.sql file after running the sql dumping script (from PHP). Normally, the dumped .sql file is outputted (written) on the server. Then when i make a href link to that file like <a href="backup.sql">Download File</a>, the file is opening inside the browser on clicking, instead of being downloading.

  • i just want to make a simple HREF LINK (to such a text file) which show up with "Save as.." dialog on simple Left Click.

How it could be done?

夏期劇場
  • 17,821
  • 44
  • 135
  • 217

4 Answers4

25

Or you could just use the new HTML5 property download in the anchor tag of your html.

The code will look something like

<a download href="path/to/the/download/file"> Clicking on this link will force download the file</a>

It works on firefox and chrome latest version. Should I mention that I didn't check it in IE? :P

Gokul N K
  • 2,428
  • 2
  • 32
  • 40
  • With the same conditions as mentioned above: the user's configuration of the browser may still prompt the user to choose between viewing and saving. When the author of the html KNOWS the file is binary and causes consternation if viewed, isn't there a way for the html author to allow only the option "Save"? It seems like the OP's question is still unanswered. Are there some file suffixes (e.g. .exe) that behave differently? – bootchk Aug 30 '13 at 11:37
4

Add the following lines to your .htaccess file.

<Files "backup.sql">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</Files>
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • This is so cool Rob. Thanks for the trick ;) It works nicely for me but if possible, can i have it with `Save as..` dialog? – 夏期劇場 Feb 09 '12 at 10:18
  • You should get a `Save As...` dialog using these rules. (If you're getting an Internal Server error, you've probably not loaded the headers module). – Rob W Feb 09 '12 at 11:16
  • No Rob. The file is now saved with just a SINGLE click into default browser download folder. But, extensively i just want the browser to ask with a dialog box to save (as like when we save a link with Right Click--> Save as). But, just asking for the knowledge. This is not really need i know. – 夏期劇場 Feb 10 '12 at 10:06
  • @4lvin >_> That's your browser configuration. The *default* behaviour **without** the headers is showing the file as plain text, without saving it. After sending the headers, the browser should treat the file as a download. Your browser is configured to save a file on download, without prompting. **In Firefox**, this preference can be changed at `Preferences > General > Downloads`. – Rob W Feb 10 '12 at 15:09
3

Another option is serving it with in a .php file eg download.php

have this in download.php

    $path = "backup.sql"
    header("Content-Type: application/octet-stream");    //

    header("Content-Length: " . filesize($path));    

    header('Content-Disposition: attachment; filename='.$path);

    readfile($path); 

then

<a href="download.php">Download File</a>
vikki
  • 2,766
  • 1
  • 20
  • 26
  • So great script, vikki. Thanks for it ;) It works nicely for me but if possible, can i have it with `Save as..` dialog? – 夏期劇場 Feb 09 '12 at 10:20
1

Just for referencing in this thread https://stackoverflow.com/a/56664507/766644

If you have all export data (your .sql text e.g.) available on client, you could just

const blob = new Blob([fileContent], { type: 'text/plain' });
const a = document.createElement('a');
a.setAttribute('download', fileName);
a.setAttribute('href', window.URL.createObjectURL(blob));
a.click();
Vaulter
  • 196
  • 2
  • 6