FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Download option instead of display





bukaida
I am using PHP 4.4 with apache 2 and mysql 5.01. In my program, I have one folder called download and keeping some text and pdf files there. Now in the php page hyperlink whenever I am calling them with a syntax like
Code:
echo'<a href="';echo $display;echo'">';echo $book_title;echo'</a>';
the file is directly displayed on the screen. I want to have the save/open prompt so that it can be downloaded.
Please help on this matter.
BTW the links are saved in mysql database.
kv
If you want to force a download, you cannot do it using a link. You have to have a script which sends a download header first, then streams the file to the browser. Then give link to your script. Sample code below.

Code:

<?php

// Send force download header
header('Content-Disposition: attachment; filename="download.txt"'); //replace with correct file name

// Send the file to browser
readfile('download.txt');
?>


You can pass the file name as parameter to the script and create link like this.

Code:

echo "<a href='downloadfile.php?filename=<?php echo $filename; ?>' ><?php $book_title; ?></a>";


So the script will be more like this

Code:

<?php

$filename=$_GET['filename'];
header('Content-Disposition: attachment; filename="'.$filename.'"'); //replace with correct file name
readfile( $filename );

?>
bukaida
Thanx man.But the name of the file is fetched from the database by select-where query.So can I use a variable instead of "download.txt"? I am having multiple download links in the same page.

Another silly question-Can I use multiple header in the same page?
kv
Look at my previous reply. In the third "code" section, there is a version which uses variable instead of hardcoded file name.

Yes, you can send multiple headers, but make sure you send all the headers before starting any output.
Related topics
2 suggestions
Moding cars
configuration in phpbb
Naruto, not really a TV show for english viewers, but still
Template help
Marvel Vs Dc
Family Guy
My Review of Google Talk 1.0.0.95 (Unreleased Version)
phpBB - Installation Tutorial
Automatic download start !!
Download database.
Where is the cheapest place to download music?
Downloadable content links
I think I just had a frihost 2.0 vision! gotta write this
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.