In PHP, the size of the file is detected based on the HTTP protocol (that is, the size of the remote file is obtained without downloading the file), using the URL function get_headers – get all the headers sent by the server to respond to an HTTP request
1. Reference URL:https://www.shuijingwanwq.com/2021/08/10/5155/. In PHP, the size of the file is detected based on the HTTP protocol (that is, the size of the remote file is obtained without downloading the file), the previous implementation, using the file system function fopen – open the file or the URL. If the URL of the file requires a 301 jump, the real size of the file cannot be obtained. as shown in Figure 1
<?php
function remote_filesize($url) {
static $regex = '/^Content-Length: *+\K\d++$/im';
// 将 $url 指定的名字资源绑定到一个流上,只读方式打开,将文件指针指向文件头
if (!$fp = @fopen($url, 'rb')) {
return 0;
}
// 执行 fopen() 时可以使用变量 $http_response_header。其中包含响应标头的数组。
if (
isset($http_response_header) &&
preg_match($regex, implode("\n", $http_response_header), $matches)
) {
print_r($http_response_header);
return (int)$matches[0];
}
return 0;
}
$startTime = microtime(true);
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4';
echo remote_filesize($url) . "\n";
$endTime = microtime(true);
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
Array
(
[0] => HTTP/1.1 301 Moved Permanently
[1] => Server: nginx
[2] => Date: Tue, 10 Aug 2021 05:16:59 GMT
[3] => Content-Type: text/html
[4] => Content-Length: 162
[5] => Connection: close
[6] => Location: https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4
[7] => Strict-Transport-Security: max-age=15768000
[8] => HTTP/1.1 200 OK
[9] => Server: nginx
[10] => Date: Tue, 10 Aug 2021 05:17:00 GMT
[11] => Content-Type: video/mp4
[12] => Content-Length: 123596884
[13] => Last-Modified: Tue, 04 Dec 2018 06:27:23 GMT
[14] => Connection: close
[15] => ETag: "5c061e4b-75df054"
[16] => Expires: Thu, 09 Sep 2021 05:17:00 GMT
[17] => Cache-Control: max-age=2592000
[18] => Strict-Transport-Security: max-age=15768000
[19] => Accept-Ranges: bytes
)
162
耗费时间:0.34205508232117秒
2. Reference URL:https://www.php.net/manual/zh/function.get-headers. Decide to use the URL function get_headers() to return an array containing the headers sent by the server in response to an HTTP request.
3. The new implementation scheme, its code is more concise and takes less time. However, if the URL of the file needs 301 jumps, the problem that the real size of the file is still exists. as shown in Figure 2
<?php
function remote_filesize($url) {
// 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。
$headers = get_headers($url, 1);
if ($headers !== false && isset($headers['Content-Length'][0])) {
print_r($headers);
return $headers['Content-Length'][0];
}
return 0;
}
$startTime = microtime(true);
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4';
echo remote_filesize($url) . "\n";
$endTime = microtime(true);
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
Array
(
[0] => HTTP/1.1 301 Moved Permanently
[Server] => Array
(
[0] => nginx
[1] => nginx
)
[Date] => Array
(
[0] => Tue, 10 Aug 2021 05:48:21 GMT
[1] => Tue, 10 Aug 2021 05:48:21 GMT
)
[Content-Type] => Array
(
[0] => text/html
[1] => video/mp4
)
[Content-Length] => Array
(
[0] => 162
[1] => 123596884
)
[Connection] => Array
(
[0] => close
[1] => close
)
[Location] => https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4
[Strict-Transport-Security] => Array
(
[0] => max-age=15768000
[1] => max-age=15768000
)
[1] => HTTP/1.1 200 OK
[Last-Modified] => Tue, 04 Dec 2018 06:27:23 GMT
[ETag] => "5c061e4b-75df054"
[Expires] => Thu, 09 Sep 2021 05:48:21 GMT
[Cache-Control] => max-age=2592000
[Accept-Ranges] => bytes
)
162
耗费时间:0.27841401100159秒
4. For the new implementation, if the URL of the file does not need 301 jumps, it does not exist: location . as shown in Figure 3
Array
(
[0] => HTTP/1.1 200 OK
[Server] => nginx
[Date] => Tue, 10 Aug 2021 05:49:20 GMT
[Content-Type] => video/mp4
[Content-Length] => 123596884
[Last-Modified] => Tue, 04 Dec 2018 06:27:23 GMT
[Connection] => close
[ETag] => "5c061e4b-75df054"
[Expires] => Thu, 09 Sep 2021 05:49:20 GMT
[Cache-Control] => max-age=2592000
[Strict-Transport-Security] => max-age=15768000
[Accept-Ranges] => bytes
)
1
耗费时间:0.17363715171814秒
5. If the URL of the file needs 301 to jump, continue to request the URL of the 301 jump to obtain the size of the file. The code is implemented as follows. The real size of the file after the jump is finally obtained. as shown in Figure 4
<?php
function remote_filesize($url) {
echo 0 . "\n";
// 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。
$filesize = 0;
$headers = get_headers($url, 1);
if ($headers !== false) {
echo 1 . "\n";
if (!isset($headers['Location']) && isset($headers['Content-Length'])) {
echo 2 . "\n";
$filesize = $headers['Content-Length'];
}
if (isset($headers['Location'])) {
echo 3 . "\n";
return remote_filesize($headers['Location']);
}
}
return $filesize;
}
$startTime = microtime(true);
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4';
echo remote_filesize($url) . "\n";
$endTime = microtime(true);
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
0
1
3
0
1
2
123596884
耗费时间:0.404137134552秒
6. If the URL of the file does not need 301 jump, the execution sequence is as follows, only 1 request. The same file: Qin Shimingyue’s first episode of Yinghuo Shouxin.mp4, the request time is equivalent, but the current plan is slightly shorter than the previous plan. Note: The time unit is in seconds. as shown in Figure 5
<?php
function remote_filesize($url) {
echo 0 . "\n";
// 返回一个数组,包含有服务器响应一个 HTTP 请求所发送的标头。
$filesize = 0;
$headers = get_headers($url, 1);
if ($headers !== false) {
echo 1 . "\n";
if (!isset($headers['Location']) && isset($headers['Content-Length'])) {
echo 2 . "\n";
$filesize = $headers['Content-Length'];
}
if (isset($headers['Location'])) {
echo 3 . "\n";
return remote_filesize($headers['Location']);
}
}
return $filesize;
}
$startTime = microtime(true);
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/秦时明月第一集荧惑守心.mp4';
echo remote_filesize($url) . "\n";
$endTime = microtime(true);
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
0
1
2
143162935
耗费时间:0.14869999885559秒




