有个需求,假设我们有个下载列表,需要遍历文件大小,但是不实际下载,如何处理呢?以下给出PHP curl 检测下载文件大小示例代码:

<?php
function getsize($url)
{
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$okay = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
if (isset($matches[1])) {
$size = $matches[1];
} else {
$size = 'unknown';
}
$last_mb = round($size/(1024*1024),3);
$last_kb = round($size/1024,3);
return "{$last_kb} KB\t{$last_mb} MB";
}

相关参考:
https://blog.csdn.net/xuxujian/article/details/7237006



Related Posts: PHP curl 检测下载文件大小示例 :

留言