Problem downloading files > 50MB

I'm using the following code for users to download files from my website. It works fine for files under 50MB, but fails immediately for larger files. Can someone explain why this is happening and how to fix it? Some of the downloadable files are over 800MB, so I need this working.

Thanks in advance

<?php
 error_reporting(E_ALL);
 ini_set('display_errors', '1');

$php_scripts = '../../php/';
require $php_scripts . 'PDO_Connection_Select.php';
require $php_scripts . 'GetUserIpAddr.php';

$ip = GetUserIpAddr();
if (!$pdo = PDOConnect("foxclone_data"))
{   echo "unable to connect";
   exit;
}

function mydloader($l_filename=NULL){    
    if( isset( $l_filename ) ) {
        $filename = $mysqli->real_escape_string($l_filename));

        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if  ($ext == '.deb')
            header('Content-Type: octet-stream');
        elseif ($ext == '.iso')
            header('Content-Type: application/x-cd-image');
        elseif ($ext =='.gz')
            header('Content-Type: application/zip');
        else
            header('Content-Type: octet-stream');

            header('Content-Description: File Transfer');
            header("Content-Disposition: attachment; filename={$filename}");
            header('Pragma: public');
            header('Expires: 0');    
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filename));   
            readfile($filename);


        // Get lookup id
        $test = $pdo->query("SELECT lookup.id FROM lookup WHERE inet_aton('$ip') >= lookup.ipstart AND inet_aton('$ip') <= lookup.ipend");
        $ref = $test->fetchColumn();
        $ref = intval($ref);

        // Insert record in download table
        $stmt = $pdo->prepare("INSERT INTO download (`address`, `filename`, `ip_address`, `lookup_id`) VALUES (?, ?, inet_aton('$ip'),?)");
        $stmt->execute([$ip, $ext, $ref]) ; 
      }

    else {
        echo "isset failed";
        }  
}      
mydloader($_GET["f"]);