Implementation principle of PHP file Upload progress

Source: Internet
Author: User
The uploadprogress extension depends on JS to obtain the progress provided by the server. the progress here is the progress of the files received by the server.

The uploadprogress extension depends on JS to obtain the progress provided by the server. the progress here is the progress of the files received by the server.

After PHP5.4, we can obtain the file upload progress from the session data without adding extensions. The uploadprogress extension and the session extension of PHP5.4 can both get the upload progress. Is there any difference between them?

Let's take a look at the uploadprogress extension, download the source code package, solve the problem, and open the file directly. we can find a simple example in example. In the info. php file, the uploadprogress_get_info function is used to obtain the upload progress. The upploadprogress. c file stores the extended implementation process. The key to uploadprogress extension implementation lies in its module sleep functions:

1234567 PHP_MINIT_FUNCTION (uploadprogress) {REGISTER_INI_ENTRIES (); php_rfc1867_callback = uploadprogress_php_rfc1867_file; returnSUCCESS ;}

The core of this function is to set php_rfc1867_callback to uploadprogress_php_rfc1867_file.

How can I set this function pointer?

In the previous article, when obtaining the file upload type in the PHP kernel, we learned that the PHP function for processing the POST request is SAPI_POST_HANDLER_FUNC (rfc1867_post_handler) (main/rfc1867.c ). Here, we have found several calls to php_rfc1867_callback. from the first parameter of the call, it can be divided into six events, or six callback update points.

If we search for php_rfc1867_callback when viewing the PHP 5.4 session extension implementation file session. c, you will find a value assignment operation similar to the extension in the module initialization function:


1 Php_rfc1867_callback = php_session_rfc1867_callback;

Similarly, the php_session_rfc1867_callback function processes the same six events as uploadprogress. these six events are equivalent to six hook programs, which correspond to six different locations for processing POST requests respectively, in PHP5.4, their functions are as follows:


1. before processing all request entities, MULTIPART_EVENT_START initializes the upload progress information, which is similar to the progress structure information (such as content-length) used to record the upload progress information)


2. MULTIPART_EVENT_FORMDATA executes this initialization operation for the control contained in each multipart. before this operation, it will parse the Content-Disposition attributes and initialize other information of progress, such as session_id, and the key of the entire Upload activity, which indicates that the entire Upload progress is ready.


3. MULTIPART_EVENT_FILE_START starts to process the uploaded file information. if the progress data does not exist, this structure is created, initialize the start_time, content_length, bytes_processed, files, and other information stored in the session. Then process the upload attributes of a single file, such as field_name and tmp_name. Initialize fields such as tmp_name. In this step, the information about the upload progress starts only when the session value is obtained.


4. MULTIPART_EVENT_FILE_DATA updates the length of the uploaded file. before detecting a pile of file-related information and writing a temporary file, it is also before writing data to $ _ FILES.


5. MULTIPART_EVENT_FILE_END: after a single file is uploaded, some information about the file will be updated. for example, the error, tmp_name, and tmp_name fields are null at start. Of course, the done field of the current file is updated here.

6. MULTIPART_EVENT_END: Update the final information of the session array, such as the done Field and clear the progress information,

The six events here are the same, while the uploadprogress extension and the session extension of PHP5.4 have some differences in the intermediate storage structure and the final returned content and methods during event processing. The storage structure of the uploadprogress extension is a temporary file generated according to the extended rules. finally, an array of the upload progress is returned through the extended function uploadprogress_get_info. The storage structure of PHP5.4 is the SESSION storage method, file, or memcache. this is based on the session settings, and the related array is finally returned through $ _ SESSION.


Besides the uploadprogress extension, APC also sets php_rfc1867_callback = apc_rfc1867_progress to provide a similar solution. set apc in ini. rfc1867 is enabled, and a hidden domain APC_UPLOAD_PROGRESS is added to the form. The value of this field can generate a random hash to determine the uniqueness of this Upload operation. Use Ajax to call the interface for displaying progress on the server, and use the apc_fetch function to obtain the upload progress of files cached by APC. For example, print_r (apc_fetch ("upload _ $ _ POST [APC_UPLOAD_PROGRESS]"); the following result is obtained:

1234567891011 Array ([total] => 1142543 [current] => 1142543 [rate] => 1828068.8 [filename] => test [name] => file [temp_filename] =>/tmp/ php8F [cancel_upload] => 0 [done] => 1)

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.