参考链接: https://segmentfault.com/q/1010000000721799
通过curl方式获取压缩包名:
function getFile($url, $save_dir = '', &$filename = '', $type = 0) { if (trim($url) == '') {
return false;
}
if (trim($save_dir) == '') {
$save_dir = './';
}
if (0 !== strrpos($save_dir, '/')) {
$save_dir.= '/';
}
//创建保存目录
if (!file_exists($save_dir) && !mkdir($save_dir, 0777, true)) {
return false;
}
//获取远程文件所采用的方法
if ($type) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); $tmpInfo = curl_exec($ch);
list($headers, $body) = explode("\r\n\r\n", $tmpInfo, 2); // echo $headers; $header_array = explode("\n", $headers); // print_r($header_array); foreach($header_array as $header_value) { $header_pieces = explode(':', $header_value);
if(count($header_pieces) > 1) { for ($i=0; $i <(count($header_pieces)/2) ; $i++) {
$headersArr[$header_pieces[$i*2]] = trim($header_pieces[2*$i+1]);
} }
} // print_r($headersArr); $file_name = $headersArr['Content-Disposition']; // echo $file_name; $filename = exec("echo '$file_name' | awk -F \"=\" '/filename/{print $2}'",$output,$ret); // echo $filename; // print_r($output);
// $file_type = $headersArr['Content-Type'];
$content = $body; // echo $file_name."<br>";
// print_r($headers); // $content = curl_exec($ch);
curl_close($ch);
} else {
ob_start();
readfile($url);
$content = ob_get_contents();
ob_end_clean();
} // print_r($_SERVER); //echo $content;
$size = strlen($content);
//文件大小
$fp2 = @fopen($save_dir . $filename, 'a');
fwrite($fp2, $content);
fclose($fp2);
unset($content, $url);
return array(
'file_name' => $filename,
'save_path' => $save_dir . $filename,
'file_size' => $size
); }
2、curl -O -d "key=val" url
这样返回的内容就保存为同名文件了。