curl_multi_close()调用时出现总线错误

时间:2014-02-04 14:16:57

标签: php curl ftp download libcurl

PHP脚本用于从FTP服务器下载文件。但当我关闭多处理程序时,php cli终端显示“总线错误”并终止进程。

哪些行会导致此错误?如何解决?

function download ($host, array $files, $threadsCount = 5) {
    $filesCount = count($fileNames);
    $threadsCount = $filesCount < $threadsCount ? $filesCount : $threadsCount;

    $mh = curl_multi_init();
    $fh = [];

    // Default options for all threads
    $options = [
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FOLLOWLOCATION => 1,
        CURLOPT_MAXREDIRS      => 5,
        CURLOPT_HEADER         => 0,
        CURLOPT_BINARYTRANSFER => 1,
        CURLOPT_TIMEOUT        => 200,
        CURLOPT_CONNECTTIMEOUT => 200,
    ];

    // Init first threads
    for ($i = 0; $i < $threadsCount; $i++) {
        $src = $fileNames[$i];

        // Path to file on server
        $url = $host . '/' . $src;

        // Where to store this file
        $tmp = '/tmp/' . $src;

        $ch = curl_init();
        $fh[$url] = fopen($tmp, 'wb');

        $files[$src] = $tmp;

        curl_setopt_array($ch, $options + [
            CURLOPT_URL  => $url,
            CURLOPT_FILE => $fh[$url]
        ]);

        curl_multi_add_handle($mh, $ch);
    }

    // Process threads
    do {
        while (($state = curl_multi_exec($mh, $running)) == CURLM_CALL_MULTI_PERFORM);

        if ($state != CURLM_OK) {
            break;
        }

        // Request is completed
        while ($done = curl_multi_info_read($mh)) {
            // Start a new request before killing old
            if ($i < $filesCount) {
                $src = $fileNames[$i];

                $url = $host . '/' . $src;
                $tmp = '/tmp/' . $src;

                $ch = curl_init();
                $fh[$url] = fopen($tmp, 'wb');

                curl_setopt_array($ch, $options + [
                    CURLOPT_URL  => $url,
                    CURLOPT_FILE => $fh[$url]
                ]);

                curl_multi_add_handle($mh, $ch);
                $i++;
            }

            // Close handlers for files and cURL
            fclose($fh[ curl_getinfo($done['handle'], CURLINFO_EFFECTIVE_URL) ]);

            curl_multi_remove_handle($mh, $done['handle']);
            curl_close($done['handle']);
        }

        if ($running) {
            curl_multi_select($mh, 10);
        }

    } while ($running);

    // AFTER THIS "BUS ERROR" IS FIRING!
    curl_multi_close($mh);
}

对于某些服务器,此错误不会出现。

1 个答案:

答案 0 :(得分:0)

这听起来像libcurl库或PHP / CURL绑定中的错误。如果您没有使用最新版本,则您有理由进行升级。

如果您使用的是最新版本,则有理由直接将这个问题与项目联系起来。