PHP通过curl下载文件到浏览器

public function downFile($url, $file_name)
{
    header('Content-Description: File Transfer');
    header('Content-Type: application/vnd.android.package-archive');
    header('Content-Disposition: attachment; filename=' . $file_name);
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
    curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $buffer) {
        echo $buffer;
        return strlen($buffer);
    });
    curl_exec($ch);
    curl_close($ch);
}

直接下载到浏览器!非常的方便!

posted @ 2020-06-22 13:41  TBHacker  阅读(1562)  评论(0编辑  收藏  举报