codeigniter文件上传问题
生活随笔
收集整理的這篇文章主要介紹了
codeigniter文件上传问题
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
codeigniter自帶的文件下載輔助函數(shù)非常簡(jiǎn)單實(shí)用,但是在處理大文件的時(shí)候,就顯得捉襟見(jiàn)肘。
在網(wǎng)上找到了一個(gè)對(duì)download_helper.php文件的擴(kuò)展,非常好用,記錄下,遇到相同問(wèn)題的猿友們可以借鑒下。
代碼如下:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /*** Force Download** Generates headers that force a download to happen** @access public* @param string filename* @param mixed the data to be downloaded* @return void*/ if ( ! function_exists('force_download')) {function force_download($filename = '', $file = ''){if ($filename == '' OR $file == ''){return FALSE;}// Try to determine if the filename includes a file extension.// We need it in order to set the MIME typeif (FALSE === strpos($filename, '.')){return FALSE;}// Grab the file extension$x = explode('.', $filename);$extension = end($x);// Load the mime typesif (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/mimes.php')){include(APPPATH.'config/'.ENVIRONMENT.'/mimes.php');}elseif (is_file(APPPATH.'config/mimes.php')){include(APPPATH.'config/mimes.php');}// Set a default mime if we can't find itif ( ! isset($mimes[$extension])){$mime = 'application/octet-stream';}else{$mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];}// Generate the server headersif (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== FALSE){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: ".filesize($file));}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: ".filesize($file));}readfile_chunked($file);die;} }/*** readfile_chunked** Reads file in chunks so big downloads are possible without changing PHP.INI** @access public* @param string file* @param boolean return bytes of file* @return void*/ if ( ! function_exists('readfile_chunked')) {function readfile_chunked($file, $retbytes=TRUE){$chunksize = 1 * (1024 * 1024);$buffer = '';$cnt =0;$handle = fopen($file, 'r');if ($handle === FALSE){return FALSE;}while (!feof($handle)){$buffer = fread($handle, $chunksize);echo $buffer;ob_flush();flush();if ($retbytes){$cnt += strlen($buffer);}}$status = fclose($handle);if ($retbytes AND $status){return $cnt;}return $status;} }/* End of file MY_download_helper.php */ /* Location: ./application/helpers/MY_download_helper.php */小提示:
@ 使用的時(shí)候,別忘了先加載
$this->load->helper('download');
@ 該擴(kuò)展和原生的force_download($filename = '', $data = '')函數(shù)的第二個(gè)參數(shù)有所不同
原生的$data為一個(gè)字符串,而該函數(shù)的$file為需要下載的文件的物理路徑!大概是因?yàn)閒read()只能正確讀取全路徑的文件的緣故吧,沒(méi)有求證,知道的猿友請(qǐng)幫忙解釋下,謝謝!
轉(zhuǎn)載于:https://www.cnblogs.com/shaoyikai/p/3837490.html
總結(jié)
以上是生活随笔為你收集整理的codeigniter文件上传问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: location.href、locati
- 下一篇: 计算机科学综合常用链接