php断点上载
content
header
range
size
php断点下载
?
<?php /** * PHP-HTTP断点续传实现 * @param string $path: 文件所在路径 * @param string $file: 文件名 * @return void */ function download($path, $file) { $real = $path . '/' . $file; if (! file_exists ( $real )) { return false; } $size = filesize ( $real ); $size2 = $size - 1; $range = 0; if (isset ( $_SERVER ['HTTP_RANGE'] )) { header ( 'HTTP /1.1 206 Partial Content' ); $range = str_replace ( '=', '-', $_SERVER ['HTTP_RANGE'] ); $range = explode ( '-', $range ); $range = trim ( $range [1] ); header ( 'Content-Length:' . $size ); header ( 'Content-Range: bytes ' . $range . '-' . $size2 . '/' . $size ); } else { header ( 'Content-Length:' . $size ); header ( 'Content-Range: bytes 0-' . $size2 . '/' . $size ); } header ( 'Accenpt-Ranges: bytes' ); header ( 'application/octet-stream' ); header ( "Cache-control: public" ); header ( "Pragma: public" ); //解决在IE中下载时中文乱码问题 $ua = $_SERVER ['HTTP_USER_AGENT']; if (preg_match ( '/MSIE/', $ua )) { $ie_filename = str_replace ( '+', '%20', urlencode ( $file ) ); header ( 'Content-Dispositon:attachment; filename=' . $ie_filename ); } else { header ( 'Content-Dispositon:attachment; filename=' . $file ); } $fp = fopen ( $real, 'rb+' ); fseek ( $fp, $range ); while ( ! feof ( $fp ) ) { set_time_limit ( 0 ); print (fread ( $fp, 1024 )) ; flush (); ob_flush (); } fclose ( $fp ); } /*End of PHP*/
登入後複製
ob_start(); $size = filesize($file_dir . $file_name); // 输入文件标签 Header("Content-type: application/octet-stream"); Header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); Header("Content-length: " . $size); $encoded_filename = urlencode($down_name); $encoded_filename = str_replace("+", "%20", $encoded_filename); $ua = $_SERVER["HTTP_USER_AGENT"]; // 处理下载的时候的文件名 if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); } else if (preg_match("/Firefox/", $ua)) { header('Content-Disposition: attachment; filename*="utf8\'\'' . $down_name . '"'); } else { header('Content-Disposition: attachment; filename="' . $down_name . '"'); } $fp = fopen($file_dir . $file_name, "r"); // 打开文件 if (isset($_SERVER['HTTP_RANGE']) && ($_SERVER['HTTP_RANGE'] != "") && preg_match("/^bytes=([0-9]+)-/i", $_SERVER['HTTP_RANGE'], $match) && ($match[1] 0 ? ($size - $range) : 0; header("Content-Length:" . $rangesize); header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size); // header("Connection: close" . "\n\n" ); //header("Connection: close"." "); } else { header("Content-Length: " . (string) ($size)); header("Accept-Ranges: bytes"); $range = 0; header("Content-Range: bytes " . $range . '-' . ($size - 1) . "/" . $size); } fpassthru($fp); ob_end_flush(); fclose($fp); exit;
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章
R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前
By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前
By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
擊敗分裂小說需要多長時間?
3 週前
By DDD
R.E.P.O.保存文件位置:在哪里以及如何保護它?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)