温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何编写php支持分块与断点续传文件下载功能代码

发布时间:2021-09-30 16:23:37 来源:亿速云 阅读:141 作者:iii 栏目:开发技术

这篇文章主要介绍“如何编写php支持分块与断点续传文件下载功能代码”,在日常操作中,相信很多人在如何编写php支持分块与断点续传文件下载功能代码问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何编写php支持分块与断点续传文件下载功能代码”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

复制代码 代码如下:

$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) {
//指定位置到结束
if ($start >= $fsize || $start < 0) {
$start = 0;
$end = $fsize;
} else {
$end = $fsize – $start;
}
} else if (! $isset_start && $isset_end) {
//最后n个字节
$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 ‘avi' :
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 ‘application/javascript';
case ‘json' :
case ‘xml' :
case ‘zip' :
case ‘pdf' :
case ‘rtf' :
case ‘tar' :
return ‘application/' . $fileSuffix;
case ‘swf' :
return ‘application/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 ‘application/x-rar-compressed';
case ‘exe' :
case ‘msi' :
return ‘application/x-msdownload';
case ‘cab' :
return ‘application/vnd.ms-cab-compressed';
case ‘aif' :
return ‘audio/aiff';
case ‘mpg' :
case ‘mpe' :
case ‘mp3′ :
return ‘audio/mpeg';
case ‘mpeg' :
case ‘wav' :
case ‘aiff' :
return ‘audio/' . $fileSuffix;
case ‘qt' :
case ‘mov' :
return ‘video/quicktime';
case ‘psd' :
return ‘image/vnd.adobe.photoshop';
case ‘ai' :
case ‘eps' :
case ‘ps' :
return ‘application/postscript';
case ‘doc' :
case ‘docx' :
return ‘application/msword';
case ‘xls' :
case ‘xlt' :
case ‘xlm' :
case ‘xld' :
case ‘xla' :
case ‘xlc' :
case ‘xlw' :
case ‘xll' :
return ‘application/vnd.ms-excel';
case ‘ppt' :
case ‘pps' :
return ‘application/vnd.ms-powerpoint';
case ‘odt' :
return ‘application/vnd.oasis.opendocument.text';
case ‘ods' :
return ‘application/vnd.oasis.opendocument.spreadsheet';
default :
if (function_exists ( ‘mime_content_type' )) {
$fileSuffix = mime_content_type ( $filename );
} else {
$fileSuffix = ‘application/octet-stream';
}
return $fileSuffix;
break;
}
}

到此,关于“如何编写php支持分块与断点续传文件下载功能代码”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI