PHP uses session to implement upload progress

Source: Internet
Author: User
Tags file upload progress bar

Implementation of the file upload progress bar is basically relying on JS plug-in or HTML5 file API to complete, in fact, PHP with Ajax can also achieve this function.

PHP Manual for the session upload progress is described as follows:

The following principles are introduced:
When the browser uploads a file to the server, PHP will store the details of the file upload (such as upload time, upload progress, etc.) in the session. Then, as the upload progresses, the information in the session is updated periodically. In this way, the browser can use Ajax to periodically request a server-side script that returns progress information from the session, and the browser-side JavaScript will be able to display/update the progress bar based on that information.

PHP.ini you need to configure the following options

session.upload_progress.enabled = "1" session.upload_progress.cleanup = "1" session.upload_progress.prefix = "Upload_ Progress_ "session.upload_progress.name =" php_session_upload_progress "session.upload_progress.freq =" 1% " Session.upload_progress.min_freq = "1"

wherein the Enabled control Upload_progress function is open or not, by default;
Cleanup set the file upload when the request is completed, whether to clear the SESSION information, by default, if you need to debug $_session, it should be set to off.
The prefix and name two entries are used to set the variable name/key name stored in the session for progress information.
The Freq and min_freq two items are used to set the server-side update frequency for progress information. A reasonable set of these two items can reduce the burden on the server.
In the form where the file is uploaded, you need to set an identifier for the upload and use that identifier in the next procedure to refer to the progress information.

Specifically, you need to have a hidden input in the upload form whose Name property is the value of session.upload_progress.name in php.ini; its value is an identifier defined by yourself. As follows:
The code is as follows:

<input type= "hidden" name= "<?php Echo ini_get (' Session.upload_progress.name ');?>" value= "test"/>

After receiving the file Upload form, PHP creates a new key in the $_session variable, which is a string that will be obtained after connecting the Session.upload_progress.prefix value to the above custom identifier, which can be obtained as follows:
The code is as follows:

$name = Ini_get (' session.upload_progress.name '); $key = Ini_get (' Session.upload_progress.prefix '). $_post[$name];$_session[$key]; This is the progress information of this file upload.

$_session[$key] The structure of this variable is this:

Array (' upload_progress_test ' = = Array (' start_time ' = + 1491494993,   //Start time ' content_length ' = 1410397,  //POST request total data length ' bytes_processed ' + 1410397,//Received data length ' done ' = true,               //Request completed TRUE indicates complete, false not completed ' Files ' = = Array (0 = = Array (' field_name ' = ' file1 ', ' name ' = = ' test.jpg ', ' tmp_name ' = ' d:\\wamp\\tmp\\php ') E181.tmp ', ' error ' = 0, ' done ' = true, ' start_time ' = 1491494993, ' bytes_processed ' and ' = 1410096, '),);


In this way, we can use the Content_length and bytes_processed two items to get the progress percentage.
After the introduction of the principle, we will complete the implementation of a PHP and JavaScript-based file upload progress bar.


Upload Form index.php

<?php session_start ();? ><! DOCTYPE html>

Note the Session.upload_progress.name hidden item in the form, and the value is set to test. There is only one file upload input in the form, and you can add multiple if necessary.
Here, you need to pay particular attention to the target property of the form, which is set to an IFRAME in the current page. This is critical to avoid the current page jumps by setting the target property so that the page after the form is submitted is displayed in the IFRAME. Because we still have to show the progress bar on the current page.

Uploading Files upload.php

<?php/** * Upload file */if (is_uploaded_file ($_files[' file1 ' [' tmp_name '])) {//unlink ($_files[' file1 '] [' tmp_name ']); $ FileName = ' pic_ '. Date (' Ymdhis '). Mt_rand (10000,99999); $ext = substr ($_files[' file1 ' [' name '], Strrpos ($_files[' file1 ' [' Name '], '. ')); Move_uploaded_file ($_files[' file1 ' [' tmp_name '], $fileName. $ext);}

 

Ajax get upload Progress progress.php

<?php/** * Ajax Get upload file Progress */session_start (); $i = Ini_get (' session.upload_progress.name ');//session.upload_ Progress.name = "php_session_upload_progress" $key = Ini_get ("Session.upload_progress.prefix"). $_get[$i];//session.upload_progress.prefix = "Upload_progress_". ' Test ' if (!empty ($_session[$key])) {$current = $_session[$key] ["bytes_processed"];//Received data length $total   = $_session[ $key ["Content_length"];  The total data length of the POST request echo $current < $total? Ceil ($current/$total * 100): 100;} Else{echo 100;}

Precautions:
The location of the 1.input label the input tag of name Session.upload_progress.name must be placed in front of the file input <input type= "file"/>.
2. You can cancel the upload by setting $_session[$key [' cancel_upload '] = true. However, only files that are being uploaded and files that have not yet started can be canceled. Files that have been successfully uploaded will not be deleted.
3. Fetch_progress () should be called through SetTimeout (), which ensures that the next request is not started until a request is returned. If you use SetInterval () there is no guarantee that this will cause the progress bar to ' fail back '.

PHP uses session to implement upload progress

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.