noodlehaus/dispatch

send() causes Out Of Memory with any file of any size

scan opened this issue · 2 comments

scan commented

I have a the problem that I can't make a file download path. The error I'm getting is this:

PHP Fatal error:  Allowed memory size of 8388608 bytes exhausted (tried to allocate 7344128 bytes) in vendor/dispatch/dispatch/src/dispatch.php on line 392

It doesn't matter how I set my memory limit, the function always exceeds it. I tested with up to 128M. My code is similar to this:

on('GET', '/file', function () {
    nocache();
    $db = scope('db');
    $file_id = params('id');

    $row = $db->files()->where('id', $file_id)->limit(1)->fetch();

    if($row === false) {
        error(404);
    } else {
        send($row['file_path'], $file_id . '.bin');
    }
});

Using dev-master of dispatch and the only other library in use is notORM.
I checked that $row['file_path'] exists. The smallest file I have tested it with has 500 KB, and it still occurs. Maybe I did wrong in the call?

hi scan, please update your dispatch package. this commit should fix it.

scan commented

Sorry, this update does not quite fix it: It now downloads the file, but the file is not streamed. Files bigger than the memory limit still cause it to overflow.

Perhaps switching off the output buffer entirely via ob_end_flush()?