Search code examples
phpzipdownload-manageridm

IDM (Internet Download Manager) takes x2 time to download my ZIP file


I ZIP all my reports into a single ZIP file. When I turn off IDM, the download process tooks 20 seconds. But when enable IDM, it tooks 20 seconds to show the IDM download dialog, then after I click OK, it tooks another 20 seconds.

Can I do something bout this at my PHP code so that IDM user wont suffer? Or any explanation?

This is how I create Zip file in PHP:

$zip = new ZipArchive();
$filename = "Test.zip";
if($zip->open($filename, ZipArchive::CREATE)!==TRUE) die("cannot open <$filename>\n");

foreach([1,2,3,4,5] as $id) {
    $path = dirname($_SERVER['HTTP_REFERER']) . '/myreport.php';
    $ch = curl_init($path);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['id' => $id]));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
    $get_file = curl_exec($ch);
    curl_close($ch);
    if($get_file === false) echo 'CURL ERROR: '.curl_error($ch);

    $zip->addFromString("Report $id.pdf", $get_file);
}

$zip->close();
header('Content-disposition: attachment; filename='.$filename);
header('Content-type: application/zip');
ob_clean();
readfile($filename);
unlink($filename);

die;

Solution

  • See @CBroe's comment for the answer.

    These download managers often make multiple requests simultaneously, to downloads multiple parts of the response in parallel. That probably messes with your script here, in that either the already opened ZIP file might be blocked (so the next instance of the script would have to wait, until the previous one is done and releases it again), or it simply does "double the work", and therefor it takes more time overall.
    So you would have to find a way to identify these "extra" requests, and cancel / reject them.