Source Rally PHP Community Scripts .. Sign up .. Login
This seems to work for me to make downloading of files work the right way (not opening as word document withing the browser window).
Access: Public      Tags: php, header, download, attachment
Add to favourites       Subscribe comments       Copy code       Bookmark
<?php
/*
//Example:
streamAttached('/path/to/file.txt','filename.txt');
*/

function streamAttached($path,$name)
{
    
header('Pragma: private');
    
header('Cache-control: private, must-revalidate');
    
header("Content-Type: application/octet-stream");
    
header("Content-Length: " .(string)(filesize($path)) );
    
header('Content-Disposition: attachment; filename="'.($name).'"');
    
readfile($path);
    exit;
}
?>
Add to favourites       Subscribe comments       Copy code       Bookmark
Sign up to add your own comment here!

Comments

neptho - 15-09-2007 00:26
Add to friends       Mail user
Unless you really want to send the file with a different filename, I'd remove the second variable, and just use htmlentities(basename($path)) for the filename.
More comments: 1
Shared by:

regin

Mail user Add to friends
All user contributed content is available under the LGPL unless specified otherwise.
Remaining copyrights Regin Gaarsmand © 2006-2008
About SourceRally.net
Programador PHP