-1

I am trying to add a download link to a large video file (approx 300MB) on someone's site but unfortunately they're on shared hosting (i've told them they will have to upgrade if they get many people downloading it). I don't want people to have to 'Save Target As' and I usually use this code to force downloads:

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // some day in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename={$file}");
header("Content-Transfer-Encoding: binary");
readfile($file);

This works fine with smaller files but not with larger ones and even after turning errors on I get no errors and no error log. I'm sure this is to do with the shared memory limit (or possibly a timeout) but does anyone know how I go about forcing downloads of large files on shared servers, ideally without javascript as i'm sure i won't be able to set the memory limits to be high enough?

Thanks very much,

Dave

deshg
  • 1,233
  • 4
  • 27
  • 45

1 Answers1

0

The usual solution is to do the file output yourself and let the web server's own buffers handle things:

$fh = fopen($file, 'rb') or die("Unable to open $file");
while($data = fread($fh, 10240)) { // 10kbyte chunks.
   echo $data;
}
fclose($fh);
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Thanks for your reply, this also works fine with small files but has the same problem with larger ones. It appears to be loading for no more than about 3 secs then just displays an empty page? Plus am I right in thinking this is much less efficient as it means it has to read it all into memory first, or is that true of the method I was using as well? Thanks so much – deshg Sep 07 '11 at 16:22
  • You'd only be reading 10k at a time in PHP's memory space. Where it goes after that depends on the webserver you're running under. If this doesn't work, then check your server's error logs for reasons why the script's not working. – Marc B Sep 07 '11 at 16:25
  • Ah of course, sorry that was me being crazy :) The wierd thing is when i check the error log there's no mention of it, as if it's run fine and the download page is listed in the access log as expected as well, just nothing happens when you access it. I can't work out why it's not erroring. – deshg Sep 08 '11 at 18:57