Thứ Ba, 14 tháng 4, 2009

Large File Downloads Issue Solved

//From: http://codeigniter.com/forums/viewthread/109798/

//CONTROLLER
// DOWNLOAD FILE
function download() {
// LOAD FILE SIZE PLUGIN
$this->load->plugin('filesize');
// GRAB FILE NAME
$file_name = $this->uri->segment(3);
// SET DIRECT PATH TO FILE
$file_path = "securefiles/".$file_namee;
// GRAB THE FILE SIZE
$file_size = get_filesize($file_path);
// CHOOSE WHICH DOWNLOAD METHOD BASED FILE SIZE
if ($file_size >= 45) {
// BEGIN DOWNLOAD
force_download($file_name, $file_path, 'large');
} else {
// READ FILE CONTENTS
$file_data = file_get_contents("securefiles/".$file_name);
// BEGIN DOWNLOAD
force_download($file_name, $file_data, 'small');
}
}

//DOWNLOAD HELPER PORTION MODIFIED
//Added Parameter: $type - Line 44
function force_download($filename = '', $data = '', $type = '')
//Added IF / ELSE statement - Line 75
// Generate the server headers
if ($type == 'small')
{
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
header("Content-Length: ".strlen($data));
}
else
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
header("Content-Length: ".strlen($data));
}
} else {
if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: binary");
header('Pragma: public');
//header("Content-Length: ".strlen($data));
readfile($data);
}
else
{
header('Content-Type: "'.$mime.'"');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Pragma: no-cache');
//header("Content-Length: ".strlen($data));
readfile($data);
}
}
?>

Không có nhận xét nào:

Đăng nhận xét