PHP 获取远程文件的大小

$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;
}

注明:以上方法虽然方便,既能判断远程的文件是否存在,还能得到大小,但是并发

posted on 2013-02-07 10:54  lsl8966  阅读(267)  评论(0编辑  收藏  举报