您的位置:首页 > 编程语言 > PHP开发

PHP 获取远程文件的大小

2013-02-07 10:54 477 查看
$uri = 'http://localhost/ToyTest/X17-45990_1.dmg';

echo remote_filesize($uri);

function remote_filesize($uri,$user='',$pw=''){
ob_start();
$ch = curl_init($uri);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_NOBODY, 1);
if (!empty($user)&& !empty($pw)){
$headers = array('Authorization: Basic ' .base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
}
$okay = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();

ob_end_clean();

$regex = '/Content-Length:\s([0-9].+?)\s/i';
$count = preg_match($regex, $head, $matches);

if (isset($matches[1])){
$size = $matches[1];
}else{
$size = '0';
}

return $size;
}


注明:以上方法虽然方便,既能判断远程的文件是否存在,还能得到大小,但是并发
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: