Making an upload progress bar using PHP's APC module

Source: Internet
Author: User
Tags apc apc module unique id
This article mainly introduces the use of PHP APC module production upload progress bar, interested in the friend's reference, I hope to be helpful to everyone.

APC module, its full name is alternative PHP Cache. APC can cache all of the PHP code, and it can provide some memory caching capabilities. But this feature is not perfect, and there are reports of unpredictable errors if you frequently use the APC cache write feature. If you want to use this feature, you can look at the APC_FETCH,APC_ Store and several other functions related to APC cache.
Happily since 5.2, APC has joined the apc_upload_progressto solve the problem of the progress bar that has been bothering you for a long time. And it put the original upload when the temporary files all cached to the memory to change when the temporary file reached the set value automatically saved to the hard disk, effectively improve the memory utilization.
It works by giving each upload a unique ID at upload time, and when the PHP script receives an upload file, the interpreter automatically checks the hidden field named Apc_upload_progress in the $_post array, which becomes the cache variable. Store information about the upload so that the script can access the uploaded file's status information via the uploaded ID.

<!– The following is the upload form –><form enctype= "Multipart/form-data" id= "Upload_form" action= "" method= "POST" ><input type = "hidden" name= "apc_upload_progress" id= "Progress_key" value= "Upid"/> video title: <input type= "text" id= "subject" name = "Subject"/> Video description: <input type= "text" id= "content" name= "content"/> Video tag (comma-separated) <input type= "text" id= "tag "Name=" tags "/><input type=" file "id=" Upfile "name=" upfile "/><input type=" Submit "id=" Filesubmit "value=" Upload "onclick=" startprogress (' upid '); return true; " /><!– Note: The parameter in startprogress (' Upid ') is the only upload parameter you have assigned from PHP –></form><!– The following is the upload progress bar –><p id= "Upstatus" Style= "width:500px; height:30px; border:1px solid # #ffffde; Color: #796140; " ></p<p id= "Progressouter" style= "width:500px; height:20px; BORDER:3PX solid #de7e00; Display:none; " ><p id= "Progressinner" style= "position:relative; height:20px; Color: #796140; Background-color: #f6d095; width:0%; "></p></p>

The main thing is that Apc_upload_progress's hidden domain, with its script to access the current upload file status, plus a display upload status of P is good.
Here is the script that deals with Ajax, which uses the jquery framework to deliver the message in JSON.

function Getprogress (upid) {var url = "<{$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 <) {SetTimeout (function () {getprogress (upid);}, 10);} else{$ ("#upstatus"). HTML ("Video upload complete, processing data, please later ...");})} function Startprogress (upid) {$ ("#progressouter"). CSS ({display: "block"}); SetTimeout (function () {getprogress (upid) ;}, 100);}

Again down is the PHP code to read the upload status, as for the processing of the upload file can be written in accordance with the usual own.
Upload file operation function, can be written according to your own needs

function Upflvaction () {if ($_server[' Request_method ']== ' POST ') {$subject = Trim ($this->f->filter ($this->_ Request->getpost (' subject ')); $content = Trim ($this->f->filter ($this->_request->getpost (' content ')); Zend_loader::loadclass (' Custom_flvop '); $flv = new Custom_flvop; $flv->uploadflv (' Upfile ', $subject, $content); }}//This is the function that reads the UPLOAD state ~ ~ Functions Upprocessaction () {if (Isset ($_get[' Progress_key ')) {$status = Apc_fetch (' Upload_ '). $_ get[' Progress_key '); $json = Array (' per ' = = $status [' current ']/$status [' Total ']*100, ' Total ' =>round ($status [' Total ']/1024), ' Current ' =>round ($status [' Current ']/1024),); Require_once ("zend/json.php"); echo Zend_json::encode ($json); } }

OK, now you can deploy it in your own site, see if the effect is cool?

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.