Detailed descriptions of PHP resumable upload instances and resumable upload instances

Source: Internet
Author: User
Tags safety mode try catch

Detailed descriptions of PHP resumable upload instances and resumable upload instances

In this case, we want to provide the client with a resumable data transfer service,

Resumable upload is mainly the Content-Range header in the HTTP protocol. Its understanding is as follows:

Content-Range: the Range of response resources. You can mark the requested resource range in multiple requests. When the connection is disconnected and reconnected, the client only requests the undownloaded part of the resource, instead of re-requesting the entire resource, implements resumable upload. Based on this principle, thunder uses multi-threaded segments to read resources on the network and merge the resources. We will discuss how to implement resumable upload using multiple threads in PHP later. This article only implements simple resumable upload.

$ File = $ _ GET ['video']; $ size = filesize ($ file); $ size2 = $ size-1; $ range = 0; if (isset ($ _ SERVER ['HTTP _ range']) {// http_range indicates requesting a part of an object/file. Use this to implement multi-threaded download and resumable upload! 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: byte S '); header ('application/octet-stream'); header ("Cache-control: public"); header ("Pragma: public "); // solve the problem of Chinese garbled characters during download in IE $ ua = $ _ SERVER ['HTTP _ USER_AGENT ']; if (preg_match ('/MSIE/', $ ua )) {// indicates that Internet Explorer is in use. $ 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 the opened file, this function moves the file pointer from the current position forward or backward to a new position. The new position starts from the file header and is measured in bytes. If the call succeeds, 0 is returned; otherwise,-1 is returned. Note that the location after the EOF operation does not produce errors. While (! Feof ($ fp) {// feof: checks whether the object has reached the end (eof) set_time_limit (0); // controls the run time print (fread ($ fp, 1024); // read the file (which can be safely used for binary files. The second parameter specifies the maximum number of bytes to be read) ob_flush (); // refresh PHP's own buffer zone flush (); // refresh the buffer content (strictly speaking, this is only when PHP is installed as an apache Module (handler or filter, it has a practical effect. it is used to refresh the buffer zone of WebServer (which can be considered to be specific to apache .)} fclose ($ fp );

Set_time_limit () function in php

When your page contains a large amount of data, we recommend that you use set_time_limit () to control the running time. The default value is 30 s, so you need to extend the execution time.

For example, set_time_limit (800), where the number of seconds is set to 0, indicating that the program continues to run until the end of the program. If you want to stop running, you can only restart php-fpm)

For example, set_time_limit (0) indicates that the program continues to run until the end of the program. However, some of the functions in the window environment cannot be set successfully, and problems may occur in Linux, add try catch to the logic code to avoid exceptions.

Note: To run this function, you need to disable the security mode. in php. ini, set the safety mode of safe_mode = Off to Off. Otherwise, the following error will occur:
Warning: set_time_limit () [function. set-time-limit]: Cannot set time limit in safe mode in

Ps: in php. ini, you can define max_execution_time to set the maximum execution time of the PHP page.

The php configuration is displayed in the output of phpinfo.

Loaded Configuration File /etc/php.iniset_time_limit(800);

This function specifies the maximum execution time of the current php script to 800 seconds. In fact

Maximum execution time = max_execution_time value in php. ini-time when 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 running 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

Summary

The above is a detailed description of the PHP resumable upload instance introduced by the editor. I hope it will help you. If you have any questions, please leave a message. The editor will reply to you in time!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.