php - Codeigniter Force download files -
by going through codeigniter documentation, using next code forcefulness download files server.
function download($file_id){ $file = $this->uploadmodel->getbyid($file_id); //getting file details //for $file_id (all details stored in db) $data = file_get_contents($file->full_path); // read file's contents $name = $file->file_name;; force_download($name, $data); }
the code working file images, when comes case of pdf files, not working. have not tested file extensions, since not working pdf, might not work other various file types. solution?
i've had similar problems, think problem resides in mime's , headers sent browsers. i've end using code found here http://taggedzi.com/articles/display/forcing-downloads-through-codeigniter. utilize function below instead of force_download. has worked me far.
function _push_file($path, $name) { // create sure it's file before doing anything! if(is_file($path)) { // required ie if(ini_get('zlib.output_compression')) { ini_set('zlib.output_compression', 'off'); } // file mime type using file extension $this->load->helper('file'); $mime = get_mime_by_extension($path); // build headers force out file properly. header('pragma: public'); // required header('expires: 0'); // no cache header('cache-control: must-revalidate, post-check=0, pre-check=0'); header('last-modified: '.gmdate ('d, d m y h:i:s', filemtime ($path)).' gmt'); header('cache-control: private',false); header('content-type: '.$mime); // add together mime type code igniter. header('content-disposition: attachment; filename="'.basename($name).'"'); // add together file name header('content-transfer-encoding: binary'); header('content-length: '.filesize($path)); // provide file size header('connection: close'); readfile($path); // force out exit(); }
hope helps.
php codeigniter
No comments:
Post a Comment