PHP quickie - readfile equiv

Lauri lauri at ruja.ee
Mon Jun 5 12:36:19 UTC 2006


> Hi folks.
> 
> I'm trying to write a script to read details from a SQL database, insert data 
> into a file and download it to the user's browser.  If I use the readfile 
> function it works, but if I use get_file_contents and echo the file doesn't 
> get downloaded properly.  In both cases, Mozilla opens oowriter, but with 
> readfile the document appears correctly, while with get_file_contents/echo it 
> show the rich text code.
> 
> Anyone any ideas?
> 
> Gary
> 
> <? // vim: ft=php nowrap ai et 
> include_once("gsql1.inc");
> include_once("gsecurity.inc");
> 
> list($dummy,$o_id,$fname)=explode("/",$_SERVER['PATH_INFO']);
> $dets=array_shift(loadhash3("select o_id as key, * from order_details where 
> o_id=$o_id"));
> if (!$dets['o_id']) {
>   echo "Order Details Missing";
>   exit;
> }
> $contents=file_get_contents("/home/httpd/files/$fname");
> if ($contents == false) {
>   echo "Document Missing";
>   exit;
> }
> header("Content-Type: text/rtf");
> header('Content-Disposition: attachment; filename="delivery_note.rtf"');
> #readfile("/home/httpd/files/$fname");
> echo $contents;
> 

Readfile() is the correct way to do this. See manual (header()).

Lauri





More information about the fedora-list mailing list