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 file system function fopen – open the file or the URL
1. In the process of realizing the channel release, there is a situation where the files to be uploaded to the channel are too large, which leads to the failure of the upload. For example, Douyin, which is temporarily limited to 128 MB. as shown in Figure 1
2. However, the implementation logic at this stage is to first download the file based on the file’s http (absolute_url: the absolute URL of the source file of the source) to obtain the file’s relative_path: The relative path of the resource file published by the channel, and then the size of the file can be obtained. And this implementation phase is asynchronous. as shown in Figure 2
3. Now I want to optimize the user’s experience. When the interface is called, the size of the file can be detected based on the HTTP protocol (that is, the size of the file is obtained without downloading the file).
4. Refer to the file system function FileSize – get the file size:https://www.php.net/manual/zh/function.filesize.php. The simplest and most efficient implementation for getting remote filesize. The simplest and most efficient implementation of the remote file size. as shown in Figure 3
5. When executing fopen(), you can use the variable $http_response_header. It contains an array of response headers. Print the variable $HTTP_RESPONSE_HEADER. as shown in Figure 4
<?php
function remote_filesize($url) {
static $regex = '/^Content-Length: *+\K\d++$/im';
// 将 $url 指定的名字资源绑定到一个流上,只读方式打开,将文件指针指向文件头
if (!$fp = @fopen($url, 'rb')) {
return false;
}
// 执行 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];
}
echo 1;
return strlen(stream_get_contents($fp));
}
$startTime = time();
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/秦时明月第一集荧惑守心.mp4';
echo remote_filesize($url) . "\n";
$endTime = time();
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
Array
(
[0] => HTTP/1.1 200 OK
[1] => Server: nginx
[2] => Date: Mon, 09 Aug 2021 05:46:41 GMT
[3] => Content-Type: video/mp4
[4] => Content-Length: 143162935
[5] => Last-Modified: Tue, 30 Oct 2018 09:52:18 GMT
[6] => Connection: close
[7] => ETag: "5bd829d2-8887e37"
[8] => Expires: Wed, 08 Sep 2021 05:46:41 GMT
[9] => Cache-Control: max-age=2592000
[10] => Strict-Transport-Security: max-age=15768000
[11] => Accept-Ranges: bytes
)
143162935
耗费时间:0秒
6. If the variable $http_response_header does not exist, then use the function stream_get_contents to read the resource flow to a string. File size: 1.27 MB, the time-consuming test: 2 seconds, found that it is unacceptable. Determines False when the variable $HTTP_RESPONSE_header does not exist. as shown in Figure 5
<?php
function remote_filesize($url) {
static $regex = '/^Content-Length: *+\K\d++$/im';
// 将 $url 指定的名字资源绑定到一个流上,只读方式打开,将文件指针指向文件头
if (!$fp = @fopen($url, 'rb')) {
return false;
}
// 执行 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];
}
*/
echo 1;
return strlen(stream_get_contents($fp));
}
$startTime = time();
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/兔子、牛、还有一只什么哟?认不出来了.mp4';
echo remote_filesize($url) . "\n";
$endTime = time();
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
1334721
耗费时间:2秒
7. The final code is implemented as follows. as shown in Figure 6
<?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 = time();
$url = 'https://www.shuijingwanwq.com/wp-content/uploads/2021/07/ChinaJoy 2015 上海 梦幻西游.mp4';
echo remote_filesize($url) . "\n";
$endTime = time();
echo '耗费时间:' . ($endTime - $startTime) . '秒';
?>
8. When calling the interface, the size of the file is detected based on the HTTP protocol (that is, the size of the file is obtained without downloading the file), and the expected goal is finally achieved. as shown in Figure 7






