Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create downloadable link to text file?

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?

like image 774
夏期劇場 Avatar asked Feb 09 '12 09:02

夏期劇場


3 Answers

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

like image 149
Gokul N K Avatar answered Oct 19 '22 02:10

Gokul N K


Add the following lines to your .htaccess file.

<Files "backup.sql">
ForceType applicaton/octet-stream
Header set Content-Disposition attachment
</Files>
like image 45
Rob W Avatar answered Oct 19 '22 03:10

Rob W


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>
like image 3
vikki Avatar answered Oct 19 '22 04:10

vikki