我正试图从远程URL获取图像的文件大小,我正试图像这样:
$remoteUrl = $file->guid;
//remote url example: http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png
$fileSize = filesize($remoteUrl);
Run Code Online (Sandbox Code Playgroud)
但是,我得到:
filesize():http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png的统计信息失败
您可以使用HTTP标头来查找对象的大小。PHP函数get_headers()可以获取它们:
$headers = get_headers('http://myApp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png', true);
echo $headers['Content-Length'];
Run Code Online (Sandbox Code Playgroud)
这样,您可以避免下载整个文件。您还可以访问所有其他标头,例如$headers['Content-Type'],如果您要处理图像(文档),这些标头会派上用场。
该错误通常意味着提供的 URL 未返回有效的图像。当我尝试访问时,http://myapp/wp-content/uploads/2017/05/Screen-Shot-2017-05-08-at-10.35.54.png它在我的浏览器中不显示图像。仔细检查返回的 URL 是否$file->guid;正确。