溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何編寫php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼

發(fā)布時(shí)間:2021-09-30 16:23:37 來源:億速云 閱讀:168 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“如何編寫php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼”,在日常操作中,相信很多人在如何編寫php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何編寫php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

復(fù)制代碼 代碼如下:

$dowmFile = dirname ( __FILE__ ) . ‘/Nokia – Always Here.mp3′; //要下載的文件,絕對或相對
$dowmName = ‘Nokia – Always Here.mp3′;
ob_start ();
getlocalfile ( $dowmFile, $dowmName );
flush ();
ob_flush ();
function getlocalfile($fname, $filename = ”) {
$fsize = filesize ( $fname );
header ( ‘Cache-Control: public' );
header ( ‘Pragma: public' );
header ( ‘Accept-Ranges: bytes' );
header ( ‘Connection: close' );
header ( ‘Content-Type: ‘ . MIMEType ( $fname ) );
//header(‘Content-Type: application/octet-stream');
if (isset ( $filename {0} )) {
header ( ‘Content-Disposition: attachment;filename=' . $filename );
}
if ($fp = @fopen ( $fname, ‘rb' )) {
$start = 0;
$end = $fsize;
$isRange = isset ( $_SERVER ['HTTP_RANGE'] ) && ($_SERVER ['HTTP_RANGE'] != ”);
if ($isRange) {
preg_match ( ‘/^bytes=([0-9]*)-([0-9]*)$/i', $_SERVER ['HTTP_RANGE'], $match );
$start = $match [1];
$end = $match [2];
$isset_start = isset ( $start {0} );
$isset_end = isset ( $end {0} );
if ($isset_start && $isset_end) {
//分塊下載
if ($start >= $fsize || $start < 0 || $start > $end) {
$start = 0;
$end = $fsize;
} else if ($end >= $fsize) {
$end = $fsize – $start;
} else {
$end -= $start – 1;
}
} else if ($isset_start && ! $isset_end) {
//指定位置到結(jié)束
if ($start >= $fsize || $start < 0) {
$start = 0;
$end = $fsize;
} else {
$end = $fsize – $start;
}
} else if (! $isset_start && $isset_end) {
//最后n個(gè)字節(jié)
$end = $end > $fsize ? $fsize : $end;
$start = $fsize – $end;
} else {
$start = 0;
$end = $fsize;
}
}
if ($isRange) {
fseek ( $fp, $start );
header ( ‘HTTP/1.1 206 Partial Content' );
header ( ‘Content-Length: ‘ . $end );
header ( ‘Content-Ranges: bytes ‘ . $start . ‘-' . ($end + $start – 1) . ‘/' . $fsize );
} else {
header ( ‘Content-Length: ‘ . $fsize );
}
if (function_exists ( ‘fpassthru' ) && ($end + $start) == $fsize) {
fpassthru ( $fp );
} else {
echo fread ( $fp, $end );
}
} else {
header ( ‘Content-Length: ‘ . $fsize );
readfile ( $fname );
}
//@header(“Content-Type: “.mime_content_type($fname));
}
function MIMEType($fname) {
$fileSuffix = strtolower ( substr ( $fname, strrpos ( $fname, ‘.' ) + 1 ) );
switch ($fileSuffix) {
case ‘a(chǎn)vi' :
return ‘video/msvideo';
case ‘wmv' :
return ‘video/x-ms-wmv';
case ‘txt' :
return ‘text/plain';
case ‘htm' :
case ‘html' :
case ‘php' :
return ‘text/html';
case ‘css' :
return ‘text/css';
case ‘js' :
return ‘a(chǎn)pplication/javascript';
case ‘json' :
case ‘xml' :
case ‘zip' :
case ‘pdf' :
case ‘rtf' :
case ‘tar' :
return ‘a(chǎn)pplication/' . $fileSuffix;
case ‘swf' :
return ‘a(chǎn)pplication/x-shockwave-flash';
case ‘flv' :
return ‘video/x-flv';
case ‘jpe' :
case ‘jpg' :
return ‘image/jpeg';
case ‘jpeg' :
case ‘png' :
case ‘gif' :
case ‘bmp' :
case ‘tiff' :
return ‘image/' . $fileSuffix;
case ‘ico' :
return ‘image/vnd.microsoft.icon';
case ‘tif' :
return ‘image/tiff';
case ‘svg' :
case ‘svgz' :
return ‘image/svg+xml';
case ‘rar' :
return ‘a(chǎn)pplication/x-rar-compressed';
case ‘exe' :
case ‘msi' :
return ‘a(chǎn)pplication/x-msdownload';
case ‘cab' :
return ‘a(chǎn)pplication/vnd.ms-cab-compressed';
case ‘a(chǎn)if' :
return ‘a(chǎn)udio/aiff';
case ‘mpg' :
case ‘mpe' :
case ‘mp3′ :
return ‘a(chǎn)udio/mpeg';
case ‘mpeg' :
case ‘wav' :
case ‘a(chǎn)iff' :
return ‘a(chǎn)udio/' . $fileSuffix;
case ‘qt' :
case ‘mov' :
return ‘video/quicktime';
case ‘psd' :
return ‘image/vnd.adobe.photoshop';
case ‘a(chǎn)i' :
case ‘eps' :
case ‘ps' :
return ‘a(chǎn)pplication/postscript';
case ‘doc' :
case ‘docx' :
return ‘a(chǎn)pplication/msword';
case ‘xls' :
case ‘xlt' :
case ‘xlm' :
case ‘xld' :
case ‘xla' :
case ‘xlc' :
case ‘xlw' :
case ‘xll' :
return ‘a(chǎn)pplication/vnd.ms-excel';
case ‘ppt' :
case ‘pps' :
return ‘a(chǎn)pplication/vnd.ms-powerpoint';
case ‘odt' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.text';
case ‘ods' :
return ‘a(chǎn)pplication/vnd.oasis.opendocument.spreadsheet';
default :
if (function_exists ( ‘mime_content_type' )) {
$fileSuffix = mime_content_type ( $filename );
} else {
$fileSuffix = ‘a(chǎn)pplication/octet-stream';
}
return $fileSuffix;
break;
}
}

到此,關(guān)于“如何編寫php支持分塊與斷點(diǎn)續(xù)傳文件下載功能代碼”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI