PHP file upload progress bar specific ways to achieve

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags ajax analysis application based cache code echo file

When we write the file upload code, we also need to consider an important realization of the function, that is, the progress of the required file upload. Currently I know PHP file upload progress bar in two ways, one is to use APC extension module written by PHP founder Rasmus Lerdorf to achieve, the other method is to use PECL extension module uploadprogress achieve.

Specific AJAX technology-based PHP parsing PHP restrictions on the specific solution to upload file size PHP batch upload pictures specific way PHP dynamic multi-file upload specific code to share PHP generic file upload class specific analysis I give two were implemented For reference, more flexible application to modify according to their needs.

APC PHP file upload progress bar to achieve:

Install APC, refer to the official document installation, you can use the PECL module installation method is quick and easy, do not explain here

Configure php.ini, set the parameters apc.rfc1867 = 1, so APC support upload progress bar function, APC source documentation

Code example:

if ($ _SERVER [REQUEST_METHOD] == POST) {// Upload request $ status = apc_fetch (upload_. $ _POST [APC_UPLOAD_PROGRESS]); $ status [done] = 1; echo json_encode ($ status); // Output to user The end of the page ajax call, the relevant documents please find their own exit;} elseif (isset ($ _GET [progress_key])) {read the progress of the upload $ status = apc_fetch (upload _. $ _ GET [progress_key]); echo json_encode ( $ status); exit;} else {// Other code like upload form, etc}

uploadprogress module to achieve:

PECL module installation method to install the PHP file upload progress bar implementation

php.ini set uploadprogress.file.filename_template = "/tmp/upd_%s.txt"

Code example:

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 {// failed to upload} else {// upload error} elseif (! Empty ($ _ GET [sessid])) {header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified:". Gmdate ("D, d MYH: i: s"). "GMT"); header ("Cache-Control: no-store, no-cache, must-revalidate" Header ("Pragma: no-cache"); header ("Content-Type: text / html; charset = UTF-8") "Cache-Control: post-check = 0, pre-check = 0", false) ; $ unique_id = $ _GET [sessid]; $ uploadvalues ​​= uploadprogress_get_info ($ unique_id); if (is_array ($ uploadvalues)) {echo json_encode ($ uploadvalues);} else {// read progress failed, processing logic otherwise}} else {// display upload form}

The above is the specific method for uploading progress bar of two kinds of PHP files that we introduce to you.

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.