Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP » Force file download

Force file download

06/07/2011 by Dux   -   5/5 (1 ratings)

Function that forces the files download.
Sending the file size does not work in all configurations.

See a demonstration of this script in action :

Open in a new window     Open in the same window

source code



 
<?php
function download ($file)
{
   if (!
is_file($file)) return false;
   
header('HTTP/1.1 200 OK');
   
header('Date: ' . date("D M j G:i:s T Y"));
   
header('Last-Modified: ' . date("D M j G:i:s T Y"));
   
header("Content-Type: application/force-download");
   
header("Content-Lenght: " . filesize($file)); // Do not work everytime
   
header("Content-Transfer-Encoding: Binary");
   
header("Content-Disposition: attachment; filename=".str_replace(" ", "_", $file)); // remove spaces to prevent a bug.
   
readfile($file);
}
?>
Rate this code     |     Copied : 28x

Comments for Force file download

No comment
Add a comment Nickname
ZOOM


Force file download
Extract links from a page
Extract images from a page
Sound effects on an action with HTML5
Isolate a color
Simple image gallery PHP
Simple method : listing files from a directory
Make dir and upload files via FTP
Adjust the rate of image colors
Automatically adjust brightness contrast of an image