这里写自定义目录标题
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;
}