首頁  >  文章  >  後端開發  >  php檔下載 重新命名

php檔下載 重新命名

不言
不言原創
2018-04-20 15:04:262900瀏覽

這篇文章介紹的內容是關於php檔案下載重新命名,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

php檔案下載重新命名

下載本機檔案

$file_url = “./本地路径”
$out_filename = ‘下载后自动保存的名字’; 
if(!file_exists($file_url)) {
    echo "不存在";
}else{
       header('Accept-Ranges: bytes');
       header('Accept-Length: ' . filesize( $file_url ));
       header('Content-Transfer-Encoding: binary');
       header('Content-type: application/octet-stream');
       header('Content-Disposition: attachment; filename=' . $out_filename);
       header('Content-Type: application/octet-stream; name=' . $out_filename);
       if(is_file($file_url) && is_readable($file_url)){
            $file = fopen($file_url, "r");
            echo fread($file, filesize($file_url));
            fclose($file);
       }
}

下載遠端檔案

$file_ur = ‘远程文件地址’; 
$out_filename='下载后自动保存的文件名';
$file = @fopen($file_url, "r");
if($file){
    $content="";
    while(!feof($file)){//测试文件指针是否到了文件结束的位置 
        $data=fread($file,1024); 
        $content.=$data;
    }
    fclose($file); 
    $filesize = strlen($content); 
    header('Accept-Ranges: bytes');
    header('Accept-Length: ' . $filesize);
    header('Content-Transfer-Encoding: binary');
    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename=' . $out_filename);
    header('Content-Type: application/octet-stream; name=' . $out_filename);
        echo $content;
}else{
    echo "文件不存在";
}

相關推薦:

php檔案上傳error的錯誤類型


               

以上是php檔下載 重新命名的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn