0

I have a script that downloads a large file (1.3gb) using readfile().

If I create a .php page with just the script on it it works fine, but if I place the same script in a Snippet and place it on a page nothing happens.

Is ModX blocking the download some how? Any advice would be great thanks!

EDIT code:

$file = $_SERVER['DOCUMENT_ROOT']."/movie.mov";
if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
};
MeltingDog
  • 14,310
  • 43
  • 165
  • 295
  • Shouldn't `ob_clean();` come **before** the `header()` calls ? – Niloct Oct 09 '12 at 01:38
  • @Niloct maybe, but that above is the example in the PHP documentation – MeltingDog Oct 09 '12 at 03:45
  • Modx snippets can't [output](http://rtfm.modx.com/display/revolution20/Snippets#Snippets-SimpleExample) information, only `return` data. That `flush()` may be the culprit. – Niloct Oct 09 '12 at 20:18

1 Answers1

0

Modx does have a maximum file size setting [Maximum upload size upload_maxsize] but that is for the file manager. I doubt that is your problem.

let's see the script and error logs.

UPDATE

just tested your little snippet out [with a couple of minor changes] ~ it works fine.

$base_path = $modx->config['base_path'];

$movie = 'frankenweenie-mrwhiskers_r640s.mov';

$file = $base_path.$movie;


if (file_exists($file)) {

    echo 'file exists '.filesize($file);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

    return true;

}else{

    echo 'file does not exist';

    return false;

}

using the $modx->config['base_path'] is not your problem, it worked using the server vars as well, it's just a good habit. as are the returning true/false modx expects it's snippets to return something whether true, false or $output ... also, not your problem it worked without.

Start looking at your php settings, I think possibly memory limit could be the problem. Check the php docs & see if it needs to have enough memory available to read a file that size. [even though it indicates 'size doesn't matter']

enable error logging in the script itself & check the server error logs.

It works with small files? Then look here: PHP readfile() and large downloads

Good luck!

Community
  • 1
  • 1
Sean Kimball
  • 4,506
  • 9
  • 42
  • 73