PHP – Force Download Dialog Box

Question: How to force a download dialog box to appear when clicking a download link/button, instead of displaying it in the browser?

Answer: You do not need to create or write a file for a text file, or html file, or php file, or even image file. All you need to do is think of a name for the file, make a link or button and point them to this script below:

<?php

$filename = “filename.txt”;
$filecontent = “This is the text that will be written in the filename.txt. It could be anything in here, including the encoded characters of an image file.”;

header(“Content-disposition: attachment; filename=”.$filename);
header(“Content-type: text/TXT”);
header(“Pragma: no-cache”);
header(“Expires: 0”);
@set_time_limit(0);
echo $filecontent;
?>

That’s it. Easy, right? Good Luck!

This entry was posted in PHP. Bookmark the permalink.

Tinggalkan Jawapan

Please log in using one of these methods to post your comment:

WordPress.com Logo

Anda sedang menulis komen melalui akaun WordPress.com anda. Log Out / Tukar )

Twitter picture

Anda sedang menulis komen melalui akaun Twitter anda. Log Out / Tukar )

Facebook photo

Anda sedang menulis komen melalui akaun Facebook anda. Log Out / Tukar )

Google+ photo

Anda sedang menulis komen melalui akaun Google+ anda. Log Out / Tukar )

Connecting to %s