Use the APC module of PHP to create an upload progress bar

Source: Internet
Author: User
Tags apc module
Use the APC module of PHP to create an upload progress bar


It seems that PHP has never made a breakthrough in the upload progress. Yesterday I found a PHP module named APC, which is named Alternative PHP Cache. APC can cache all PHP code and provide certain memory caching functions. however, this function is not perfect. it is reported that frequent use of the write function of APC cache will lead to unexpected errors. if you want to use this function, you can look at several functions related to apc cache, such as apc_fetch and apc_store.

It is worth noting that from 5.2 onwards, APC has added a plug-in called APC_UPLOAD_PROGRESS, which solves the issue of progress bars that have plagued everyone for a long time. In addition, it caches all the temporary files during the original upload to the memory and saves them to the hard disk automatically when the temporary files reach the set value, effectively improving the memory utilization.

The function is to assign each upload a unique ID during the upload process. when the PHP script receives an upload file, the interpreter will automatically check the hidden field named APC_UPLOAD_PROGRESS in the $ _ POST array, which will become a cache variable and store information about the upload, in this way, the script can access the status information of the uploaded file through the upload ID.


XML/HTML code

12345678910111213141516


The most important thing is the hidden domain of APC_UPLOAD_PROGRESS. with this script, you can access the status of the currently uploaded File. just add a div that displays the upload status.

The following is the Ajax processing script. I use the Jquery framework to transmit messages in json format.

JavaScript code

123456789101112131415161718192021222324 FunctiongetProgress (upid) {varurl = "<{$ siteurl}> epadmin/upprocess"; $. getJSON (url, {progress_key: upid}, function (json) {$ ("# progressinner "). width (json. per + "%"); $ ("# upstatus" ).html ('file size: '+ json. total + 'KB' + 'uploaded: '+ json. current + 'KB'); if (json. per <100) {setTimeout (function () {getProgress (upid) ;}, 10) ;}else {$ ("# upstatus" ..html ("video Upload completed, processing data. please wait ...... ") ;}} FunctionstartProgress (upid) {pid (" # progressouter ").css ({display:" block "}); setTimeout (function () {getProgress (upid);}, 100 );}

The next step is to read the PHP code in the upload status. As for the process of uploading files, you can write them by yourself.

12345678910111213141516171819202122232425 // File upload operation function. you can compile functionupflvAction () {if ($ _ SERVER ['request _ method'] = 'post') as needed ') {$ subject = trim ($ this-> f-> filter ($ this-> _ request-> getPost ('subobject '))); $ content = trim ($ this-> f-> filter ($ this-> _ request-> getPost ('content'); Zend_Loader :: loadClass ('m M _ flvop'); $ flv = newCustom_FlvOp; $ flv-> uploadFlv ('upfile', $ subject, $ content );}} // This is the function used to read the upload status ~~ FunctionupprocessAction () {if (isset ($ _ GET ['SS SS _ key']) {$ status = apc_fetch ('upload _'. $ _ GET ['SS SS _ key']); $ json = array ('per '=> $ status ['stream']/$ status ['total'] * 100, & apos; total & apos; = & apos; round ($ status ['total']/1024), & apos; current & apos; = & apos; round ($ status ['current']/1024 ),); require_once ("Zend/Json. php "); echoZend_Json: encode ($ json );}}

Now, you can deploy your site and check whether the effect is cool?

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.