我正在尝试使用this answer获取文件的fileSize
这是我的PHP代码
<?php
echo filesize('LICENSE.txt');
echo filesize('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg');
?>
以上代码仅适用于本地文件,但是当我尝试获取CDN URL
的文件大小时,出现错误
Warning: filesize() [function.filesize]: stat failed for http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg in test_db_operation() (line 80 of myphp.php).
基于filesize
php手册here
Description ¶
int filesize ( string $filename )
Gets the size for the given file.
Parameters ¶
**filename
Path to the file.
**Return Values ¶
Returns the size of the file in bytes, or FALSE (and generates an error of level E_WARNING) in case of an error.
filesize
不能与URL
一起使用,或者我在这里做错了什么。请帮忙 !!!谢谢
使用此代码查找文件大小...
<?php
$remoteFile = 'http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg';
$ch = curl_init($remoteFile);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //not necessary unless the file redirects (like the PHP example we're using here)
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
$contentLength = 'unknown';
$status = 'unknown';
if (preg_match('/^HTTP\/1\.[01] (\d\d\d)/', $data, $matches)) {
$status = (int)$matches[1];
}
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
$contentLength = (int)$matches[1];
}
echo 'HTTP Status: ' . $status . "\n";
echo 'Content-Length: ' . $contentLength;
?>
Result:
HTTP Status: 302
Content-Length: 8808759
有一些获取远程文件大小的方法
1:get_headers
print_r(get_headers('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg',true));
2:卷曲
function 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 (!emptyempty($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();
echo '<br>head-->'.$head.'<----end <br>';
// gets you the numeric value from the Content-Length
// field in the http header
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
// if there was a Content-Length field, its value
// will now be in $matches[1]
if (isset($matches[1]))
{
$size = $matches[1];
}
else
{
$size = 'unknown';
}
//$last=round($size/(1024*1024),3);
//return $last.' MB';
return $size;
}
3:套接字
function getFileSize($url)
{
$url = parse_url($url);
if($fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error))
{
fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
fputs($fp,"Host:$url[host]\r\n\r\n");
while(!feof($fp))
{
$tmp = fgets($fp);
if(trim($tmp) == '')
{
break;
}
elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr))
{
return trim($arr[1]);
}
}
return null;
}
else
{
return null;
}
}
4:file_get_contents
$fCont = file_get_contents('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg');
echo strlen($fCont)/1024;
您可以使用,
getimagesize();
它将为您提供CDN图像文件的大小。
$src = "CDN_URL";
list($width, $height) = getimagesize($src);
echo "ImageUrl: ". $src . "<br/>";
echo "width: " . $width . "<br />";
echo "height: " . $height;
如果是用户可以上传的文件,
$_FILES['file']['size']
从服务器端获取文件大小。
使用jQuery,
<input type="file" id="myFile" />
$('#myFile').bind('change', function() {
//this.files[0].size gets the size of your file.
alert(this.files[0].size);
});