Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

download the text file instead of opening in the browser

i have one text file:

When i click the Download it should download and save it in my local download path.

I have tried.

window.open("data.txt");

and

header("Location:data.txt")

But both are open the text file browser it self. I what download the txt file.

Any one please help me..

Thanks

Manikandan.

like image 889
Manikandan Thangaraj Avatar asked Aug 03 '11 05:08

Manikandan Thangaraj


People also ask

How do I force a file to download instead of open in the browser using HTML?

href = data . This will cause the browser to download the file.

Why is website downloading instead of opening in browser?

There are tens of reasons which can cause this issue of downloading files instead of opening in browser. But mainly you get this issue due to poor hosting provider, any deflect in cache plugin you're using on your website, or you messed up with the . htaccess file.

How do I get Chrome to save files instead of opening?

Click on "Settings" and you'll see a new page pop up in your Chrome browser window. Scroll down to Advanced Settings, click Downloads, and clear your Auto Open options. Next time you download an item, it will be saved instead of opened automatically.


1 Answers

Try this:

$file = "data.txt";
$text = file_get_contents($file);
header("Content-Disposition: attachment; filename=\"$file\"");
echo $text;
like image 100
Sergey Ratnikov Avatar answered Sep 28 '22 08:09

Sergey Ratnikov