Detailed Explanation of PHP Breakpoint Continued Transmission Example

  • 2021-08-12 02:23:36
  • OfStack

In doing a case, we should give the client a breakpoint continuous transmission service.

Breakpoint continued transmission is mainly the Content-Range header in HTTP protocol. It is understood as follows:

Content-Range: Range of response resources. The requested resource range can be marked in multiple requests. When the connection is disconnected and reconnected, the client only requests the part of the resource that has not been downloaded, instead of re-requesting the whole resource, thus realizing breakpoint continuous transmission. Thunderbolt is based on this principle, using multi-thread segmentation to read the resources on the network, and finally merge. We will discuss PHP's use of multithreading to realize breakpoint continuous transmission later. This paper only implements simple breakpoint continuous transmission.


$file = $_GET['video'];
$size = filesize($file);
$size2 = $size-1;
$range = 0;
if(isset($_SERVER['HTTP_RANGE'])) { //http_range Indicate a request 1 Entities / Documentary 1 Parts , Use this to realize multithreaded download and breakpoint continuation! 
 header('HTTP /1.1 206 Partial Content');
 $range = str_replace('=','-',$_SERVER['HTTP_RANGE']);
 $range = explode('-',$range);
 $range = trim($range[1]);
 header('Content-Length:'.$size);
 header('Content-Range: bytes '.$range.'-'.$size2.'/'.$size);
} else {
 header('Content-Length:'.$size);
 header('Content-Range: bytes 0-'.$size2.'/'.$size);
}
header("Content-type: video/mp4");
header('Accenpt-Ranges: bytes');
header('application/octet-stream');
header("Cache-control: public");
header("Pragma: public");
//  Solve in IE Chinese garbled code problem when downloading in 
$ua = $_SERVER['HTTP_USER_AGENT'];
if(preg_match('/MSIE/',$ua)) { // Indicates that you are using  Internet Explorer . 
 $ie_filename = str_replace('+','%20',urlencode($file));
 header('Content-Dispositon:attachment; filename='.$ie_filename);
} else {
 header('Content-Dispositon:attachment; filename='.$file);
}
$fp = fopen($file,'rb+');
fseek($fp,$range);    //fseek: Locate in an open file , This function moves the file pointer forward or backward from the current position to a new position, measured in bytes from the header of the file. Success returns  0 ; Otherwise return  -1 . Note, move to  EOF  The subsequent position will not produce errors. 
while(!feof($fp)) {    //feof: Check whether the end of the file has been reached  (eof)
 set_time_limit(0);    // Control run time 
 print(fread($fp,1024));   // Read a file (which can be safely used for 2 Binary document , No. 1 2 Parameters : Specify the maximum number of bytes to read) 
 ob_flush();      // Refresh PHP Self-buffer 
 flush();      // Refresh the contents of the buffer ( Strictly speaking ,  This is only available in PHP As a apache Adj. Module(handler Or filter) At the time of installation ,  It has a practical effect .  It is a refresh WebServer( It can be considered as specifically referring to apache) Buffer of .)
}
fclose($fp);

Application of set_time_limit () Function in php

When your page has a large amount of data, it is recommended to use set_time_limit () to control the running time. The default is 30s, so you need to lengthen the execution time.

Such as set_time_limit (800), where the number of seconds is set to 0, indicating that it continues to run until the end of the program. If you want to stop running, you can only restart php-fpm (the restart command is attached at the end of this article)

For example, set_time_limit (0) indicates that it continues to run until the end of the program, but some of this function is not set successfully under window environment, and problems may occur under Linux. Do a good job in logic code plus try catch to avoid exceptions.

Note: This function requires you to turn off Safe Mode and set safe_mode = Off Safe Mode to Off in php. ini, otherwise the following error will occur:
Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode in

ps: At php. ini, you can set the maximum execution time of an PHP page by defining max_execution_time.

You can see the php related configuration in the phpinfo () output.


Loaded Configuration File /etc/php.ini
set_time_limit(800);

This function specifies that the maximum execution time of the current php script is 800 seconds, which actually

Maximum execution time = max_execution_time value in php. ini-time the current script has been executed + set value

If max_execution_time=30 in php. ini and the current script has been executed for 5 seconds, then:

Maximum execution time = 30-5 +800 = 825 seconds.

View the php run directory command:


which php
/usr/bin/php

View the number of php-fpm processes:


ps aux | grep -c php-fpm

View running memory


/usr/bin/php -i|grep mem

Restart php-fpm


/etc/init.d/php-fpm restart

Summarize

The above is the site to introduce you to PHP breakpoint continued transmission example details, I hope to help you, if you have any questions welcome to leave me a message, this site will reply to you in time!


Related articles: