Download file script with speed limit using php | Sanwebcorner

Download file script with speed limit using php

This post i am explain about how to download file based on given speed limit. This test.zip file is downloaded based on the speed limit what we given in the code. IMAGES.zip is a target file and i fixed the 80 as a speed you can change this and then try this download speed.

download file script with speed limit


Download file script with speed Limit:


<?php

$target_file = 'IMAGES.zip';

$download_speed = 80;

if(file_exists($target_file) && is_file($target_file)) {

    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($target_file));
    header('Content-Disposition: filename='.$target_file);

    flush();

    $file = fopen($target_file, "r");

    while (!feof($file)) {

        print fread($file, round($download_speed * 1024));

        flush();

        sleep(1);
    }

    fclose($file);
}
else {
    die('Error: The file '.$target_file.' does not exist!');
}

?>


Download Code