Frage

I'm attempting to download a .mp4 file. (about 1.3GB size). I'm using following:

<?php 
$path = "video.mp4";
header('Accept-Ranges: bytes');  // For download resume
header('Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header('Content-Description: File Transfer' );
header('Content-Disposition: attachment; filename="'.basename( $path ).'"' );
header('Content-Length: ' . filesize($path));  // File size
header('Content-Transfer-Encoding: binary');  // For Gecko browsers mainly
header('Content-Type: application/octet-stream' );
header('Expires: 0' );
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($path)) . ' GMT');
header('Pragma: no-cache' );
ob_clean();
flush();
readfile($path);

I open my php file, and firefox pops up with the "want to save" menu. Size looks right. I press Save As, to my desktop. The final downloaded file, lists as a random size, around 400MB (330, 463 and 440).

Response Headers are:

Connection: Keep-Alive
Content-Disposition:    attachment; filename="//www.frederikspang.dk/elevgallavideo.mp4"
Content-Length: 1422778850
Content-Type:   video/mp4
Date:   Sun, 30 Jun 2013 22:12:30 GMT
Keep-Alive: timeout=10, max=50
Pragma: public
Server: Apache
content-transfer-encoding:  binary
War es hilfreich?

Lösung

THis is hard - most php configuration will fail after 30 seconds. If you own php.ini you can change that to longer limit. But still - is that even worth it? I mean - the files can get bigger or network slower - and once more you will hit the timeout.

This is why downloaders were made - to download big files in smaller chunks Half Crazed showed you code for that i THIS answer (its not only one - this only takes into account one of the ways clients negotiate the transfers - but still its a good start).

Mega.co.nz for example uses new html5 features. Downloads the file in browser using chunks, joining the file on user and and then ,,downloading'' it from the browser disk space. It can resume files, pause files and so on. (Sorry - no code for that as it would be quite big and include more than one language (php, js)).

PS: change yours readfile($path); into:

$handle=fopen($path, 'rb');
while (!feof($handle))
{
    echo fread($handle, 8192);
    flush();
}
fclose($handle);

This will not load WHOLE file into memory, just parts of 8KiB at once and then send them to user.

Andere Tipps

   <?php
$filename = "theDownloadedFileIsCalledThis.mp4";
$myFile = "/absolute/path/to/my/file.mp4";

// Add bellow code for mime type
$ext=strtolower(substr($fl,strrpos($myFile,".")));
$mime_types = array(
            '.txt' => 'text/plain',
            '.htm' => 'text/html',
            '.html' => 'text/html',
            '.php' => 'text/html',
            '.css' => 'text/css',
            '.js' => 'application/javascript',
            '.json' => 'application/json',
            '.xml' => 'application/xml',
            '.swf' => 'application/x-shockwave-flash',
            '.flv' => 'video/x-flv',

            // images
            '.png' => 'image/png',
            '.jpe' => 'image/jpeg',
            '.jpeg' => 'image/jpeg',
            '.jpg' => 'image/jpeg',
            '.gif' => 'image/gif',
            '.bmp' => 'image/bmp',
            '.ico' => 'image/vnd.microsoft.icon',
            '.tiff' => 'image/tiff',
            '.tif' => 'image/tiff',
            '.svg' => 'image/svg+xml',
            '.svgz' => 'image/svg+xml',

            // video
            '.3gp' => 'video/3gpp',
            '.3g2' => 'video/3g2',
            '.avi' => 'video/avi',
            '.mp4' => 'video/mp4',
            '.asf' => 'video/asf',
            '.mov' => 'video/quicktime',
        );

if (array_key_exists($ext, $mime_types)){
   $mm_type=$mime_types[$ext];
}else{
   $mm_type="application/octet-stream";
}
$mm_type="application/octet-stream";

header("Cache-Control: public, must-revalidate"); // Avoid this line
header("Pragma: public"); // Add this line
header("Pragma: hack"); // Avoid this line
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($myFile)) ); // Avoid this line
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . filesize($myFile)); // Add this line
header("Content-Transfer-Encoding: binary\n");
ob_clean(); // Add this line

readfile($myFile);

?>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top