php 利用curl 下载文件

这里写自定义目录标题

php 利用curl 下载文件

/**
     * @param $url 远端文件路径
     * @param $savePath 保存本地文件路径
     * @return string
     */
    public function downFile($url,$savePath)
    {
        //$savePath = './static';
       // $url='https://www.gia.edu/otmm_wcs_int/proxy-pdf/?ReportNumber=2344807646&url=https://myapps.gia.edu/RptChkClient/reportClient.do?ReportNumber=801CA4B84F27A558C8550B882A1864D4';


        /*HTTP/1.1 200 OK
Content-Disposition: attachment; filename=2344807646.pdf
Content-Type: application/pdf
Pragma: No-cache
Server: Oracle-HTTP-Server
X-ORACLE-DMS-ECID: 005ew_RrsjVBLAzMw2Q_MG0001Ps003daT
X-ORACLE-DMS-RID: 0:1
Content-Length: 682995
Cache-Control: max-age=172800
Expires: Mon, 03 Aug 2020 07:31:57 GMT
Date: Sat, 01 Aug 2020 07:31:57 GMT
Connection: keep-alive
X-Xss-Protection: 1; mode=block*/
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//关闭对等证书
        curl_setopt($ch, CURLOPT_URL, $url);//请求地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);//返回头部信息
        //需要response header
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);//需要response body
        $response = curl_exec($ch);
        //分离header与body
        $header = '';
        $body = '';
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
            $header = substr($response, 0, $headerSize);
            $body = substr($response, $headerSize);
        }
        curl_close($ch);
        //文件名
        $arr = array();

        if (preg_match('/filename=.*\..{3}/', $header, $arr)) {
            $ext=pathinfo($arr[0],PATHINFO_EXTENSION);//后缀名
            $file = date('YmdHis') . '.' .$ext;
            $fullName = rtrim($savePath, '/') . '/' . $file;
        //创建目录并设置权限
            $basePath = dirname($fullName);
            if (!file_exists($basePath)) {
                @mkdir($basePath, 0777, true);
                @chmod($basePath, 0777);
            }
            if (file_put_contents($fullName, $body)) {
                return $file;
            }
        }
        return false;
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值