THinkPHP中檔案上傳

來源:互聯網
上載者:User
THinkPHP中檔案下載
 THinkPHP1.5中檔案的下載 用到的系統類別庫檔案是Http.class.php,位於ThinkPHP\Lib\ORG\Net目錄下,類名Http,其中有靜態方法static function download ($filename, $showname=”,$content=”,$expire=180);/     @param string $filename 下載檔案名稱(完整路徑加檔案的儲存名字)* @param string $showname 下載顯示的檔案名稱(想要顯示的名字或者從資料庫中讀出的原來帶中文的名字);* @param string $content  下載的內容(預設為空白,此時下載的檔案就是原檔案)。* @param integer $expire  下載內容瀏覽器緩衝時間 ,預設為空白時為180秒。*/因為PHP儲存檔案名稱不支援中文,所以通常中文檔案名稱儲存到伺服器上時換成成英文名或者產生隨機名字。下載時可以利用此方法回複原檔案名稱。應用舉例:下載時顯示檔案原名/* 假設資料庫裡檔案資訊儲存表為file(id,truename,savenane,user,size).檔案存在於網站項目目錄下的uploads檔案夾裡,本網站項目名為bm,其絕對路徑為:H:\AppServ\www\bm\uploads\(  H:\AppServ\www\為主目錄)此時該目錄下有一檔案123456789.doc,(savename),原檔案名稱為“讀後感.doc”,即truename,大小為2MB.那麼要下載時伺服器端得程式為:class FileAction extends Action{public function download(){$uploadpath=’H:\AppServ\www\bm\uploads\’;//設定檔案上傳路徑,伺服器上的絕對路徑$id=$_GET['id'];//GET方式傳到此方法中的參數id,即檔案在資料庫裡的儲存id.根據之尋找檔案資訊。if($id==”) //如果id為空白而出錯時,程式跳轉到項目的Index/index頁面。或可做其他處理。{$this->redirect(‘index’,'Index’,”,APP_NAME,”,1);}$file=D(‘File’);//利用與表file對應的資料模型類FileModel來建立資料對象。$result= $file->find($id);//根據id查詢到檔案資訊if($result==false) //如果查詢不到檔案資訊而出錯時,程式跳轉到項目的Index/index頁面。或可做其他處理{$this->redirect(‘index’,'Index’,”,APP_NAME,”,1);}$savename=$file->savename;//檔案儲存名$showname=$file->truename;//檔案原名$filename=$uploadpath.$savename;//完整檔案名稱(路徑加名字)import(‘ORG.Net.Http’);Http::download($filename,$showname);}}然後在該檔案下載的HTML模板裡要下載該檔案的地方加一個下載連結,調用File模組的download方法即可。記得傳參數id .如本例中:
 
讀後感.doc sunmoon 下載
註:IE瀏覽器的下載檔案名稱編碼只有gb2312才能顯示,若是不然,要不就是檔案名稱亂碼,要不就是找不到檔案而無法下載。針對此種情況,我對原來的download()方法進行了一些調整,經過測試發現IE、傲遊、firefox均可正常下載。 /** +---------------------- * 下載檔案 * 可以指定下載顯示的檔案名稱,並自動發送相應的Header資訊 * 如果指定了content參數,則下載該參數的內容 +---------------------- * @static * @access public +---------------------- * @param string $filename 下載檔案名稱 * @param string $showname 下載顯示的檔案名稱 * @param string $content 下載的內容 * @param integer $expire 下載內容瀏覽器緩衝時間 +---------------------- * @return void +---------------------- * @throws ThinkExecption +---------------------- */ static public function download ($filename, $showname='',$content='',$expire=180) { if(file_exists($filename)){ $length = filesize($filename); }elseif(is_file(UPLOAD_PATH.$filename)){ $filename = UPLOAD_PATH.$filename; $length = filesize($filename); }elseif($content != ''){ $length = strlen($content); }else { throw_exception($filename.L('下載檔案不存在!')); } if(empty($showname)){ $showname = $filename; } $showname = basename($showname); if(empty($filename)){ $type = mime_content_type($filename); }else{ $type = "application/octet-stream"; } //發送Http Header資訊 開始下載 header("content-type:text/html; charset=utf-8"); header("Pragma: public"); header("Cache-control: max-age=".$expire); //header('Cache-Control: no-store, no-cache, must-revalidate'); header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT"); //下面一行就是改動的地方,即用iconv("UTF-8","GB2312//TRANSLIT",$showname)系統函數轉換編碼為gb2312 header("Content-Disposition: attachment; filename=". iconv("UTF-8","GB2312",$showname)); header("Content-Length: ".$length); header("Content-type: ".$type); header('Content-Encoding: none'); header("Content-Transfer-Encoding: binary" ); if($content == '' ) { readfile($filename); } else { echo($content); } exit(); }註:iconv為php系統函數庫,但需要安裝。若是伺服器還沒有這個模組安裝,則需將iconv.dll下載下來後複製到windows/system32/下面,同時在php安裝資料夾得ext檔案夾裡也複製一份。然後在php.ini檔案中將extension=php_iconv.dll前的”;”去掉,沒有的話就加上extension=php_iconv.dll。然後重啟伺服器即可。

?

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.