Re: Problem forcing a download

From: Date: Mon, 05 May 2003 20:57:04 +0000
Subject: Re: Problem forcing a download
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-146450@lists.php.net to get a copy of this message
"Kevin Stone" <kevin@helpelf.com> wrote in message news:02bc01c31347$1c335dc0$6500a8c0@kevin... > > ----- 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 > > > > Thank you for your reply Kevin, I am using //view document if(isset($_GET[view_document]) == true){ Header('Content-Type: application/octet-stream'); Header('Content-disposition: attachment; filename='.$_GET['file_name']); readfile('files/'.$_GET['file_name']); } but it seems to be downloading the contents of test.txt followed by the code for view_document.php, any ideas why this is happening? Here is the link I call with: echo '<p>Click <a href="view_document.php?file_name='.$file_name.'&file_id='.$file_id.'&projec t_name='.$_GET[project_name].'&project_id='.$_GET[project_id].'&view_documen t=true">here</a> to download the document</p>'; Thanks again.

« previous php.general (#146450) next »