Download a Text File Using PHP
Do you need to download a text file instead of having your browser read the file?
All you need to do is use the code below, and name it download.php (or whatever you want).
<?
$file = 'filename.txt';
header('Content-type: text/plain');
header('Content-Length: '.filesize($file));
header('Content-Disposition: attachment; filename='.$file);
readfile($file);
?>NOTE: Make sure you copy as is and do NOT leave any whitespace since you are sending headers.
All you need to do is replace filename.txt with the name of the filename you wish to have downloaded.
Now create another file, index.html perhaps, and just link to that php file, and you are all set. If you named the script download.php, you will just need:
<a xhref="download.php" mce_href="download.php" >Download Text File</a>Done!!
Post a Comment
You must be logged in to post a comment.