4

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);

?>
Liftoff
  • 24,717
  • 13
  • 66
  • 119
  • ftp is designed for file transfer http not so much –  Jun 09 '12 at 04:13
  • Well do you have a solution that uses ftp? I have tried it as well but every time I tried to connect and download through ftp (using PHP), the page did nothing but give me an error. – Liftoff Jun 09 '12 at 04:14
  • set up ftp server on your server, give user credentials. no php required –  Jun 09 '12 at 04:15
  • can you give me some sample PHP code? Like I said I have tried this, but every time it got to the point where it was supposed to be "downloading" the file, it returned an error. – Liftoff Jun 09 '12 at 04:16
  • He's saying to take the webserver out of the solution, and just use an FTP client to connect to an FTP server. – Marvo Jun 09 '12 at 04:22
  • Well that is not answering my question. I need this to be on a website, and not through an FTP client. – Liftoff Jun 09 '12 at 04:26
  • don't even need a ftp client as most browsers understand ftp://... so your web site can just link to ftp... –  Jun 09 '12 at 04:28
  • Okay, but how would that initiate a file download? I don't want to give the user our FTP username/password. – Liftoff Jun 09 '12 at 04:29
  • give them their own credentials –  Jun 09 '12 at 04:32
  • This is a large website, by the way. I cannot expect the hundreds of thousands of users to get the files through FTP – Liftoff Jun 09 '12 at 04:38
  • don't see why not, but that's your call, good luck. –  Jun 09 '12 at 04:46

3 Answers3

4

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.

Marcin Gil
  • 68,043
  • 8
  • 59
  • 60
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
  • 1
    These are some waters I have never stepped into before. I have no real *need* per se to use PHP, but up until this point, it was the only way I knew to download files. Can you give me some direction on how to download a file through Apache? – Liftoff Jun 09 '12 at 04:23
  • I wouldn't recommend Apache, its great but a bit hard to config, nginx is eassier, heres an example, http://kbeezie.com/view/nginx-configuration-examples/ nginx or apache sit at the base of your tech stack along with your data store which could be your file system or mysql, all they do is serve stuff, most of the times data comes from php using either FCGI (ie dynamic HTML) or just static files (images, video files), most projects define a folder with all their static content and tell nginx to serve it, without going through PHP, use PHP to track the names and dir of the files, for gen links. – Samy Vilar Jun 09 '12 at 06:31
  • If you still need to use Apache because you either have a LAMP or WAMP stack then simply add the document root to where you have all the files ... DocumentRoot /srv/appname/web Order deny,allow Allow from all – Samy Vilar Jun 09 '12 at 07:12
0

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.

goat
  • 31,486
  • 7
  • 73
  • 96
0

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.
Community
  • 1
  • 1
Ismael Vacco
  • 1,010
  • 1
  • 10
  • 24