PHP File upload progress bar two ways to implement the Code _php skills

Source: Internet
Author: User
Tags apc documentation file upload file upload progress bar php file upload
There are two kinds of methods I know at the moment, one is implemented using the APC extension module written by the founder of PHP Rasmus Lerdorf (HTTP://PECL.PHP.NET/PACKAGE/APC), and the other is to use the PECL extension module Uploadprogress implementation (http://pecl.php.net/package/uploadprogress) Here are two examples of implementation, and more flexible applications are modified to suit their needs.

APC Implementation method:

Install APC, refer to official documentation installation, you can use the Pecl module installation method quickly and easily, here does not indicate
Configure php.ini, set parameters apc.rfc1867=1, enable APC support upload progress bar function, in the APC source code description document contains instructions
code example:
Copy Code code 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 Ajax calls in the client page, please find the documentation yourself
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 uploading forms, etc.
}

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:
Copy Code code 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 successful
} else {
Upload failed
} else {
Upload Error

} elseif (!empty ($_get[' sessid ')) {
Header ("Expires:mon, June June 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 failure, additional processing logic
}

} else {
Show Upload Form
}

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.