This example uses a helper to download a very large file, of 200 MB, which can't be downloaded in one file reading.
This example will be constructed from the following helpers:
application/helpers/my_download_helper.php
: This is the CI helper that is used to download a very large fileapplication/controllers/classg2.php
: This is the controller that uses the my_download
helperapplication/views/classg2view.php
: This is the view that has a link for the file downloadLet us assume that the URI to the project root is http://mydomain.com/myproject
. Hence the URI to execute the auth
controller for login will be http://mydomain.com/myproject/classg2
.
This helper is used to download very large files, which can't be downloaded in one file reading. Its function, download_large_files
, reads 1 MB in each loop until it downloads the whole file.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'), /*** CodeIgniter Download Helpers** @package CodeIgniter* @subpackage Helpers* @category Helpers* @author Yehuda Zadik*/ // -------------------------------------------------------------- /*** Download large files** Generates headers that force a download to happen** @access public* @param string $fullPath* @return void*/ function download_large_files($fullPath){ // File Exists? if( file_exists($fullPath) ){ // Parse Info / Get Extension $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = strtolower($path_parts["extension"]); // Determine Content Type switch ($ext) { case "pdf": $ctype = "application/pdf"; break; case "exe": $ctype = "application/octet-stream"; break; case "zip": $ctype = "application/zip"; break; case "doc": $ctype = "application/msword"; break; case "xls":$ctyp = "application/vnd.ms-excel"; break; case "ppt": $ctype = "application/vnd.ms-powerpoint"; break; case "wmv": $ctype = "video/x-ms-wmv"; break; case "gif": $ctype = "image/gif"; break; case "png": $ctype = "image/png"; break; case "jpeg":case "jpg": $ctype = "image/jpg"; break; default: $ctype = "application/force-download"; } $file_handle = fopen($fullPath, "rb"); header('Content-Description: File Transfer'), header("Content-Type: " . $ctype); header('Content-Length: ' . $fsize); header('Content-Disposition: attachment; filename=' . basename($fullPath)); while(!feof($file_handle)) {$buffer = fread($file_handle, 1*(1024*1024)); echo $buffer; ob_flush();flush(); //These two flush commands seem to have helped with performance } fclose($file_handle); } else{die('File Not Found'), } } /* End of file my_download_helper.php *//* Location: ./application/helpers/my_download_helper.php */
The controller loads the helper my_download
and calls its function, download_large_files,
in order to enable the user to download large files that originally could not be downloaded, using the my_download
helper.
<?php class Classg2 extends CI_Controller { public function index() { $this->load->helper('url'), $this->load->view('classg2view'), } function download(){ // Loading the helpers url, my_download $this->load->helper(array('url', 'my_download')); // FCPATH is a constant that Codeigniter sets which // contains the absolute path to index.php $fullPath = FCPATH . 'files/movie-classg2.wmv'; // Using the helper my_download function to download // a very large filedownload_large_files($fullPath); } } /* End of file classg2.php *//* Location: ./application/controllers/classg2.php */
The view file displays the data that contains a link for downloading the very large file.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Download large file</title> </head> <body> <div id="container"> <a href="<?php echo base_url("classg2/download") ?>">Download large file</a> </div> </body> </html>
3.144.30.236