Tuesday, November 24, 2009

Download File in php

code for help to download any file using php script.
just pass complete path or pass as variable to code it will download that file to user desktop.

// $_REQUEST ['getfile'] get file name with path which want to download


if ($_REQUEST ['getfile']){
$file = $_REQUEST ['getfile'];

//$file is a variable that hold file path and name value.
}

$save_as_name = basename($file); //$save_as_name is the name by which wile will save to desktop here we take original file name


ini_set('session.cache_limiter', '');
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: no-cache');
header("Content-Type: application/octet-stream"); //defile that we work with file
header("Content-Disposition: disposition-type=attachment; filename=\"$save_as_name\"");

readfile($file);

?>

1 comment: