How to create a downloadable link to a text file?

The goal is to load the dumped backup.sql file after running the sql dumping script (from PHP). Usually the downloaded .sql file is output (written) to the server. Then, when I create an href link for this file, for example, <a href="backup.sql">Download File</a> , the file opens inside the browser when clicked, not when downloaded.

  • I just want to make a simple HREF LINK (to such a text file) that is displayed using the Save As .. dialog on a simple left Click .

How can I do that?

+6
source share
3 answers

Add the following lines to your .htaccess file.

 <Files "backup.sql"> ForceType applicaton/octet-stream Header set Content-Disposition attachment </Files> 
+3
source

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

The code will look something like this:

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

It works with the latest version of Firefox and Chrome. Should I mention that I have not tested it in IE ?: P

+17
source

Another option is to service it in a .php file, such as download.php

this is 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> 
+3
source

Source: https://habr.com/ru/post/908027/


All Articles