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

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

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

上一篇:攻防世界-Web-ics-05


下一篇:asp.net core2 mvc 基础教程--View 视图