我想获取远程文件的大小。可以通过以下代码完成:
$headers = get_headers("http://addressoffile", 1);
$filesize= $headers["Content-Length"];
但是我不直接知道文件地址。但是我有一个重定向到原始文件的地址。
例如:我有地址http://somedomain.com/files/34
当我将此地址放入浏览器的网址栏或使用function时file_get_contents('myfile.pdf',"http://somedomain.com/files/34");
,它开始下载原始文件。
如果我使用上述函数来计算文件大小,则使用该地址http://somedomain.com/files/34
返回大小0。
有没有办法获得http://somedomain.com/files/34
重定向的地址。
或任何其他用于计算重定向文件(原始文件)大小的解决方案。
如果站点重定向通过。您可以使用的Location标头:
// get the redirect url
$headers = get_headers("http://somedomain.com/files/34", 1);
$redirectUrl = $headers['Location'];
// get the filesize
$headers = get_headers($redirectUrl, 1);
$filesize = $headers["Content-Length"];
请注意,此代码不应在生产中使用,因为不会检查现有的阵列键或错误处理。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句