From 70ee8ac2a43fc2452171bbd3ddc3ab9c0ebde9cb Mon Sep 17 00:00:00 2001 From: denis Date: Mon, 25 May 2026 14:02:36 +0000 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=B8=D1=82?= =?UTF-8?q?=D1=8C=20download.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- download.php | 96 +++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 42 deletions(-) diff --git a/download.php b/download.php index f19b6cd..99cec5c 100644 --- a/download.php +++ b/download.php @@ -3,49 +3,61 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); -// Ваш путь до файла -$pathToBigVideoFile = __DIR__ . '/uploads/DSC_4170.MOV'; - if(!file_exists($pathToBigVideoFile)) { - die("Файл не найден {$pathToBigVideoFile}"); +/*** +* +* Загрузка файла частями +* +*/ - } - - $fileSize = filesize($pathToBigVideoFile); - $mimetype="application/x-rar-compressed"; -$filename = $pathToBigVideoFile; -if (!file_exists($filename)) die('Файл не найден'); -$from=$to=0; $cr=NULL; -if (isset($_SERVER['HTTP_RANGE'])) {$range=substr($_SERVER['HTTP_RANGE'], strpos($_SERVER['HTTP_RANGE'], '=')+1); -$from=strtok($range, '-');$to=strtok('/'); -if ($to>0) $to++; -if ($to) $to-=$from;header('HTTP/1.1 206 Partial Content'); -$cr='Content-Range: bytes ' . $from . '-' . (($to)?($to . '/' . $to+1):filesize($filename)); -} else header('HTTP/1.1 200 Ok'); -$etag=md5($filename); -$etag=substr($etag, 0, 8) . '-' . substr($etag, 8, 7) . '-' . substr($etag, 15, 8); -header('ETag: "' . $etag . '"'); -header('Accept-Ranges: bytes'); -header('Content-Length: ' . (filesize($filename)-$to+$from)); -if ($cr) header($cr); -header('Connection: close'); -header('Content-Type: ' . $mimetype); -header('Last-Modified: ' . gmdate('r', filemtime($filename))); -$f=fopen($filename, 'r'); -header('Content-Disposition: attachment; filename="' . basename($filename) . '";'); -if ($from) fseek($f, $from, SEEK_SET); -if (!isset($to) or empty($to)) {$size=filesize($filename)-$from; -} else {$size=$to; -} +public function downloadBigFile($filename){ -$downloaded = 0; -while( (!feof($f)) && (connection_status()==0) && ($downloaded < $size) ) { + + $mimetype="application/x-rar-compressed"; + if (!file_exists($filename)) + die('Файл не найден:'.$filename); + + $from=$to=0; + $cr=NULL; + + if (isset($_SERVER['HTTP_RANGE'])) { + $range=substr($_SERVER['HTTP_RANGE'], strpos($_SERVER['HTTP_RANGE'], '=')+1); + $from=strtok($range, '-');$to=strtok('/'); + + if ($to>0) $to++; + if ($to) $to-=$from;header('HTTP/1.1 206 Partial Content'); + $cr='Content-Range: bytes ' . $from . '-' . (($to)?($to . '/' . $to+1):filesize($filename)); + } else + header('HTTP/1.1 200 Ok'); -$block = min(1024*8, $size - $downloaded); -print(fread($f, $block)); -$downloaded += $block; -flush(); -} - -fclose($f); -exit; \ No newline at end of file + $etag=md5($filename); + $etag=substr($etag, 0, 8) . '-' . substr($etag, 8, 7) . '-' . substr($etag, 15, 8); + header('ETag: "' . $etag . '"'); + header('Accept-Ranges: bytes'); + header('Content-Length: ' . (filesize($filename)-$to+$from)); + if ($cr) + header($cr); + + header('Connection: close'); + header('Content-Type: ' . $mimetype); + header('Last-Modified: ' . gmdate('r', filemtime($filename))); + $f=fopen($filename, 'r'); + header('Content-Disposition: attachment; filename="' . basename($filename) . '";'); + if ($from) + fseek($f, $from, SEEK_SET); + if (!isset($to) or empty($to)) { + $size=filesize($filename)-$from; + } else { + $size=$to; + } + + $downloaded = 0; + while( (!feof($f)) && (connection_status()==0) && ($downloaded < $size) ) { + $block = min(1024*8, $size - $downloaded); + print(fread($f, $block)); + $downloaded += $block; + flush(); + } + + fclose($f); + } \ No newline at end of file