PHP如何判断网络上的文件的大小?

来源:百度知道 编辑:UC知道 时间:2024/06/17 13:20:28
我用php做了一个抓取某网站上的图片的程序,可是有个问题就是有时下载的图片不能够完全显示,虽说保存成的文件时jpg,但是大小只有几百个字节,因此也根本不显示,就是文件没有完全下载完,因此我想如果能够获取远程url文件的大小,当下载完毕时取得下载的文件的大小,将这两个大小值进行比对,如果大小相等,就执行下一次循环,如果不等,则continue,我网上查了些,很少找到关于获取网络文件大小的程序可查看啊,希望高手给点建议,我业余搞PHP的!请大家不要见笑!
一楼的,通过filesize获取文件大小的文件只能是通过该php服务器检查的,filesize不适用于获取网络上的文件的大小!

哈哈,长见识了,我开始做的时候,直接用ob_get_contents()的返回值来判断,我一直怕不准,才上来提问的,请高手能够解释一下如果我用ob_get_contents()来得到的文件大小准不准,如果不准,不准的原因应该是读取过程中发生丢包了吧,像有些图片上面是图片,下面是一块灰色的东西一样一个道理,请问我的怀疑对吗?

unction remote_filesize($uri,$user='',$pw='')
{
// start output buffering
ob_start();
// initialize curl with given uri
$ch = curl_init($uri);
// make sure we get the header
curl_setopt($ch, CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch, CURLOPT_NOBODY, 1);
// if auth is needed, do it here
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);
// get the output buffer
$head = ob_get_contents();
// clean the output buffer and return to previous
// buffer settings
ob_end_clean();

// gets you the numeric value from the Content-Length
// field in the http header
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = p