IDM(Internet 下载管理器)需要两倍的时间来下载我的 ZIP 文件

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

我将所有报告压缩到一个 ZIP 文件中。当我关闭 IDM 时,下载过程需要 20 秒。但是启用IDM时,显示IDM下载对话框需要20秒,然后我点击确定后,又需要20秒。

我可以在我的 PHP 代码中做一些事情,这样 IDM 用户就不会受到影响吗?或者有什么解释吗?

这就是我在 PHP 中创建 Zip 文件的方式:

$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;



====================================== =================编辑

好的,这是我基于@CBroe 评论的最终解决方案。

基本上IDM 会发出多个请求。下载速度只会提高现有文件。但就我而言,它生成的文件导致 IDM 打开的连接越多,消耗的时间就越多。

所以第一次收到请求时,我将生成的文件保存在服务器上。然后我在几个间隔后单独请求删除服务器上的文件。

解决方法如下:

$zip = new ZipArchive();
$filename = "Test.zip";

if(file_exists($filename])) {
    header('Content-disposition: attachment; filename='.$filename]);
    header('Content-type: application/zip');
    ob_clean();
    readfile($filename]);
    die;
}

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);

makeAnotherCurlThatWillUnlinkFileAfter1Minute();

die;

查看 @CBroe 的评论以获得答案。

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.