Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save file onclick

Tags:

html

php

save

I want to be able to download the csv file on click and not open in the browser

I put this code

<a href="file.csv">download file</a>

but on click it opens thev file in the browser. In localhost, when I click the link it is being downloaded but when on the server it's opening in the browser

like image 786
Osama Ahmad Avatar asked Jul 26 '12 07:07

Osama Ahmad


2 Answers

You have to set headers in order to inform web-browser for opening file-save dialog.

download.php


  <?php
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=sample.csv");
    readfile('file.csv');
   ?>

showlinks.php


<a href="download.php">Download file</a>
like image 115
KV Prajapati Avatar answered Oct 19 '22 02:10

KV Prajapati


HTML5 gives you another way - download attribute. The problem is currently only Chrome supports it. It will take some time till it will become widely used solution.

So for now I would recommend you @AVD's PHP code.

like image 2
MFix Avatar answered Oct 19 '22 03:10

MFix