天天看點

php檔案重命名下載下傳代碼示例

 php檔案重命名下載下傳代碼示例

Bata1.0、基礎代碼(file_exists僅支援本地,還有下載下傳時檔案名亂碼問題)

<?php 

$file = 'success.jpg'; 

if (file_exists($file)){ 

    header('Content-Description: File Transfer'); 

    header('Content-Type: application/octet-stream'); 

    header('Content-Disposition: attachment; filename='.basename($file)); 

    header('Content-Transfer-Encoding: binary'); 

    header('Expires: 0'); 

    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 

    header('Pragma: public'); 

    header('Content-Length: ' . filesize($file)); 

    ob_clean(); 

    flush(); 

    readfile($file); 

    exit; 

?> 

Bata2.0、解決中文名顯示亂碼問題(但還是僅支援伺服器本地)

//$down_url=$server_name.$renamefile_url.'/'.$renamefile_name;        //file_exists僅支援本地 

$down_url=$renamefile_url.'/'.$renamefile_name; 

//$sourcefile_name = iconv("UTF-8",'GBK',$sourcefile_name); 

$sourcefile_name = urlencode($sourcefile_name); 

$sourcefile_name = str_replace("+", "%20", $sourcefile_name);  

if(file_exists($down_url)){ 

    $ua = $_SERVER["HTTP_USER_AGENT"];  

    //header('Content-Disposition: attachment; filename='.basename($sourcefile_name)); 

    if (preg_match("/MSIE/", $ua)) { 

    header('Content-Disposition: attachment; filename="' . $sourcefile_name . '"'); 

    } else if (preg_match("/Firefox/", $ua)) { 

    header('Content-Disposition: attachment; filename*="utf8\'\'' . $sourcefile_name . '"'); 

    } else { 

    }  

    header('Content-Length: ' . filesize($down_url)); 

    readfile($down_url); 

else{ 

    echo '檔案目錄不存在'; 

      本文轉自許琴 51CTO部落格,原文連結:http://blog.51cto.com/xuqin/1097932,如需轉載請自行聯系原作者