Liftoff
Liftoff

Reputation: 25412

PHP Downloading a Huge Movie File (500 MB) with cURL

Okay, I have a problem that I hope you can help me fix.

I am running a server that stores video files that are very large, some up to 650 MB. I need a user to be able to request this page and have it download the file to their machine. I have tried everything, but a plain readfile() request hangs for about 90 seconds before quitting and gives me a "No data received error 324 code," a chunked readfile script that I have found from several websites doesn't even start a download, FTP through PHP solutions did nothing but give me errors when I tried to get the file, and the only cURL solutions that I have found just create another file on my server. That is not what I need.

To be clear I need the user to be able to download the file to their computer and not to the server.

I don't know if this code is garbage or if it just needs a tweak or two, but any help is appreciated!

<?php

$fn = $_GET["fn"];

echo $fn."<br/>";

$url  = $fn;
$path = "dl".$fn;

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);

?>

Upvotes: 4

Views: 2778

Answers (3)

Samy Vilar
Samy Vilar

Reputation: 11130

I wouldn't recommend serving large binary files using PHP or any other scripting technology for that matter. They where never design for this -- you can use apache, nginx or whatever standard http server you have on the back end. If you still need to use PHP, then you should probably check out readfile_chunked.

http://php.net/readfile#48683 and here's a great tutorial. http://teddy.fr/blog/how-serve-big-files-through-php

good luck.

Upvotes: 4

Ismael Vacco
Ismael Vacco

Reputation: 1030

I see two problem that can happen:

  • First: Your web server may be closed the connection by timeout. you should look the web server config.
  • Second: Timeout with curl. I recommend to see this post.

Upvotes: 0

goat
goat

Reputation: 31834

readfile() doesnt buffer. However, php itself might buffer. Turn buffering off

while (ob_get_level())
    ob_end_clean();
readfile($file);

Your web server might buffer. Turn that off too. How you do it depends on the webserver, and why its buffering.

Upvotes: 0

Related Questions