PHP File Upload progress bar Two implementation methods example code

Source: Internet
Author: User
Tags apc file upload progress bar php file upload
There are two ways I know now, one is to use the APC extension (HTTP://PECL.PHP.NET/PACKAGE/APC) written by the founder of Php Rasmus Lerdorf, and the other is to use the PECL extension module Uploadprogress implementation (http://pecl.php.net/package/uploadprogress) I'll give you two examples to implement separately for reference, more flexible applications to modify according to their own needs.

APC Implementation method:

Install APC, refer to the Official document installation, you can use the Pecl module installation method quick and simple, here does not explain
Configure the php.ini, set the parameter apc.rfc1867=1, enable APC to support the upload progress bar function, in the APC source code document contains the description
code example:

The code is as follows:

if ($_server[' request_method ' = = ' POST ') {  //upload request      $status = Apc_fetch (' upload_ '. $_post[' Apc_upload_ PROGRESS ']);      $status [' done '] = 1;      echo Json_encode ($status);  Output to the user-side page Ajax calls, related documents please look for      exit,  } elseif (Isset ($_get[' Progress_key ')) {   //Read upload progress      $status = APC _fetch (' Upload_ '. $_get[' Progress_key ');      echo Json_encode ($status);      Exit;  } else {      //other code, such as upload form  }

Uploadprogress Module Implementation method:

Install the module using the PECL module installation method
PHP.ini inside set uploadprogress.file.filename_template = "/tmp/upd_%s.txt"
code example:

The code is as follows:

if ($_server[' Request_method ']== ' POST ') {if (Is_uploaded_file ($_files[' upfile ' [' tmp_name '])) {$upload _dir          = ' your_path/';          $ext = STRRCHR ($_files[' video ' [' Name '], '. ');          $sessid = $_post[' upload_identifier '); $tmpfile = $upload _dir.           $sessid; $sessfile = $upload _dir.          $sessid. $ext; if (Move_uploaded_file ($_files[' upfile ' [' tmp_name '], $tmpfile)) {//upload succeeded} else {//upload lost Fail} else {///Upload Error} elseif (!empty ($_get[' sessid ')) {header ("Expires:mon, Jul 1997 05:00:00 GMT"      ); Header ("last-modified:".) Gmdate ("D, D M Y h:i:s").      "GMT");      Header ("Cache-control:no-store, No-cache, must-revalidate");      Header ("Cache-control:post-check=0, pre-check=0", false);      Header ("Pragma:no-cache");      Header ("Content-type:text/html;charset=utf-8");      $unique _id = $_get[' sessid ');      $uploadvalues = Uploadprogress_get_info ($unique _id); if (is_aRray ($uploadvalues)) {echo json_encode ($uploadvalues); else {//Read progress failed, additional processing logic}} else {//Show upload form}
Related Article

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.