ModX Evo: PHP readfile() in snippet?

507 Views Asked by At

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;
};
1

There are 1 best solutions below

0
On

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!