gpt4 book ai didi

php 文件大小不适用于 URL(CDN URL)

转载 作者:行者123 更新时间:2023-12-02 01:03:12 24 4
gpt4 key购买 nike

我正在尝试使用 this answer 获取文件的文件大小

这是我的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 一起使用,或者我在这里做错了什么。请帮忙 !!!谢谢

最佳答案

有一些方法可以获取远程文件大小

1:get_headers

print_r(get_headers('http://cdn.bloody-disgusting.com/wp-content/uploads/2013/10/Victoria-Justice-HD-Wallpapers.jpg',true)); 

2: curl

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;

关于php 文件大小不适用于 URL(CDN URL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25909565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com