日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

codeigniter文件上传问题

發(fā)布時間:2025/3/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codeigniter文件上传问题 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

codeigniter自帶的文件下載輔助函數(shù)非常簡單實用,但是在處理大文件的時候,就顯得捉襟見肘。

在網(wǎng)上找到了一個對download_helper.php文件的擴展,非常好用,記錄下,遇到相同問題的猿友們可以借鑒下。

代碼如下:

<?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 */

小提示:

  @ 使用的時候,別忘了先加載

    $this->load->helper('download');

  @ 該擴展和原生的force_download($filename = '', $data = '')函數(shù)的第二個參數(shù)有所不同

    原生的$data為一個字符串,而該函數(shù)的$file為需要下載的文件的物理路徑!大概是因為fread()只能正確讀取全路徑的文件的緣故吧,沒有求證,知道的猿友請幫忙解釋下,謝謝!

    

轉(zhuǎn)載于:https://www.cnblogs.com/shaoyikai/p/3837490.html

總結(jié)

以上是生活随笔為你收集整理的codeigniter文件上传问题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。