PHP文件断点续传类

2016-05-21 10:47:10 -0400
该类在个人PHP程序:https://kaike.wodemo.net/file/430523中使用,使用方法:
<?php
include "FileDownload.class.php";
$d = new FileDownload();
$d -> download("./test.mp3");//$d -> download("./test.mp3", time() . ".mp3", "audio/mpeg", true, true)
?>

代码如下:

<?php
/*
* File download class(文件断点续传下载类)
* author Kaike Network
* date 2016/03/28 18:35:08
* ver 1.1
* site https://kaike.wodemo.net
*/

class fileDownload {

/*
* $file -- 文件路径
* $name -- 文件名
* $type -- MIME类型
* $reload -- 是否打开断点续传
* $isname -- header头是否插入文件名
*/
public function download($file, $name = "", $type = "", $reload = true, $isname = true) {
if (file_exists($file)) {
if (empty($name)) {
$name = explode("/", $file);
$name = array_pop($name);
}
if (empty($type)) {
$type = $this -> getMime($name);
}
$size = filesize($file);
$range = $this -> getRange($size);
$fp = fopen($file, "rb");
if (is_array($range) && $reload) {
header("HTTP/1.1 206 Partial Content", true, "206");
header("Content-Length: " . (($range["end"] - $range["start"]) + 1));
header("Content-Range: bytes " . $range["start"] . "-" . $range["end"] . "/" . $size);
fseek($fp, $range["start"]);
} else {
header("HTTP/1.1 200 OK", true, "200");
header("Content-Length: " . $size);
}
if ($isname == true) {
header("Content-Disposition: attachment; filename=" . $name);
}
header("Cache-Control: no-cache");
header("Pragma: no-cache");
header("Content-Type: " . $type);
header("Accept-Ranges: bytes");
while (!feof($fp)) {
echo fread($fp, 1024);
if (ftell($fp) == $size || ftell($fp) == $range["end"]) {
break;
}
}
fclose($fp);
} else {
header("HTTP/1.1 404 Not Found", true, "404");
return null;
}
}

/*
* $name -- 从字符串中提取文件MIME类型
* $mime -- MIME数组
*/
private function getMime($name, $mime = "") {
$ex = explode(".", $name);
$type = array_pop($ex);
if (empty($mime)) {
$mime = array(
"apk" => "application/vnd.android.package-archive",
"bmp" => "image/bmp",
"jpg" => "image/jpeg",
"png" => "image/png",
"jpeg" => "image/jpeg",
"gif" => "image/gif",
"ico" => "image/x-icon",
"svg" => "image/svg+xml",
"mp3" => "audio/mpeg",
"mp4" => "video/mp4",
"js" => "text/javascript",
"css" => "text/css",
"json" => "application/json",
"htm" => "text/html",
"html" => "text/html",
"txt" => "text/plain",
"jar" => "application/java-archive",
"zip" => "application/zip",
"tar" => "application/x-tar",
"gz" => "application/x-gzip",
"rar" => "application/rar",
"tgz" => "application/x-compressed"
);
}
return (empty($mime[$type])) ? "application/octet-stream" : $mime[$type];
}

/*
* $size -- 文件大小
*/
private function getRange($size) {
$getrange = preg_replace("/(\r\n|\r|\n|\x20)/", "", $_SERVER["HTTP_RANGE"]);
if (isset($getrange) && !empty($getrange)) {
preg_match("/bytes=([\-\,0-9]+)/i", $getrange, $range);
$rangea = explode(",", $range[1]);
$ranges = explode("-", $rangea[0]);
$rgs["start"] = is_numeric($ranges[0]) ? $ranges[0] : 0;
$rgs["end"] = is_numeric($ranges[1]) ? $ranges[1] : ($size - 1);
return $rgs;
} else {
return null;
}
}
}
?>
«Newer      Older»

Back to home

Subscribe | Register | Login | N