php 从 URL 获取远程文件的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5958725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get size of remote file from URL?
提问by Buffon
Possible Duplicate:
PHP: Remote file size without downloading file
可能的重复:
PHP:不下载文件的远程文件大小
I want to find the size of a file from PHP when the user enters the link of the file which he uploaded on a different site. But how?
当用户输入他上传到不同站点的文件的链接时,我想从 PHP 中找到文件的大小。但是如何?
回答by Steven Ross
I assume you want the size of a remote file. The following PHP code should do the trick:
我假设您想要远程文件的大小。以下 PHP 代码应该可以解决问题:
<?php
// URL to file (link)
$file = 'http://example.com/file.zip';
$ch = curl_init($file);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
// Contains file size in bytes
$contentLength = (int)$matches[1];
}
?>
You first need to obtain the URL to the file which, in relation to MediaFire and other file sharing websites, would need extra code to obtain after satisfying the free user waiting period.
您首先需要获取文件的 URL,对于 MediaFire 和其他文件共享网站,在满足免费用户等待期后需要额外的代码才能获取。