Re: Problem forcing a download

From: Date: Mon, 05 May 2003 20:44:22 +0000
Subject: Re: Problem forcing a download
Groups: php.general 
Request: Send a blank email to php-general+get-146448@lists.php.net to get a copy of this message
----- Original Message ----- From: "shaun" <shaun@mania.plus.com> To: <php-general@lists.php.net> Sent: Monday, May 05, 2003 2:03 PM Subject: Re: [PHP] Problem forcing a download > "Kevin Stone" <kevin@helpelf.com> wrote in message > news:029a01c31339$ebe605d0$6500a8c0@kevin... > > > > ----- Original Message ----- > > From: "shaun" <shaun@mania.plus.com> > > To: <php-general@lists.php.net> > > Sent: Monday, May 05, 2003 12:46 PM > > Subject: [PHP] Problem forcing a download > > > > > > > Hi, > > > > > > I am trying to force licks to files to open a file save dialog box, > > whatever > > > the file type is. > > > > > > I have the following link on my page (the page links back to itself): > > > > > > <p>Click <a > > > > > > href="view_document.php?file_name='.$file_name.'&view_document=true">here</a > > > > to download the document</p>'; > > > > > > I also include a file at the top of each page (top.php) for function > calls > > > and in this file I have the following: > > > > > > //view document > > > if(isset($_GET[view_document]) == true){ > > > Header('Content-Type: application/octet-stream'); > > > Header('Content-disposition: attachment; filename='.$_GET[file_name]); > > > } > > > > > > when I click on the link I get the dialog box popping up but it > downloads > > > the code for view_document.php. > > > > > > I am obviously doing something silly here but I can't work it out, I > would > > > be very grateful for you advice here. > > > > When are you outputing the file? readfile($_GET['file_name']); after > > header. > > - Kevin > > > > > > thank you for your reply, but where do i put readfile($_GET['file_name']); > The headers set stdout to the download stream. So all you have to do is read in then output the desired file after the headers are set. The readfile() function conveniently does this in one step. But you could also use fopen(), read the file into a string, and then echo that string. Or any one of a dozen other methods for outputting content. The point is whatever content is sent to stdout (or outputed to the screen) is going to be contained in the download stream. if(isset($_GET['view_document'])) { header('Content-Type: application/octet-stream'); header('Content-disposition: attachment; filename='.$_GET['file_name']); readfile($_GET['file_name']); // <- assuming the file and script are in the same directory } HTH, Kevin

« previous php.general (#146448) next »