当我下载视频文件时,我得到零字节文件大小。以下是视频下载的代码。需要一些帮助
// Check if file exist
if (!file_exists($path)) {
header('HTTP/1.1 404 Not Found');
exit();
}
// Determine file mimetype
$finfo = new finfo(FILEINFO_MIME);
$mime = $finfo->file($path);
// File size
$size = filesize($path);
// Check if we have a Range header
if (isset($_SERVER['HTTP_RANGE'])) {
// Parse field value
list($specifier, $value) = explode('=', $_SERVER['HTTP_RANGE']);
// Can only handle bytes range specifier
if ($specifier != 'bytes') {
header('HTTP/1.1 400 Bad Request');
return;
}
// Set start/finish bytes
list($from, $to) = explode('-', $value);
if (!$to) {
$to = $size;
}
// Response header
header('HTTP/1.1 206 Partial Content');
header('Accept-Ranges: bytes');
// Response size
header('Content-Length: ' . ($to - $from));
// Range being sent in the response
header("Content-Range: bytes ".$from."-".($to-1)."/{$size}");
// Open file in binary mode
$fp = fopen($path, 'rb');
// Advance to start byte
fseek($fp, $from);
// Send the data
while(true){
// Check if all bytes have been sent
if(ftell($fp) >= $to){
break;
}
// Send data
echo fread($fp, 8192);
// Flush buffer
flush();
ob_flush();
}
} else {
// If no Range header specified, send everything
header('Content-Length: ' . $size);
// Send file to client
readfile($path);
}
} else {
$path = "$media_dir/$filename";
header("Content-Type: video/mp4");
}
}