PHP modules make use of APC upload progress bar

Renderings: 63151341919304   like PHP has been no breakthrough in the upload progress in this regard. Yesterday found a PHP module called APC, which stands for Alternative PHP Cache. APC can all PHP code is cached, it may provide some additional memory caching, but this feature is not quite perfect, there are reports that if you frequently use the APC cache write function, can lead to unexpected errors. If want to use this feature, you can take a look at apc_fetch, apc_store several other functions associated with the apc cache. I am pleased that APC from 5.2 start adding stuff called APC_UPLOAD_PROGRESS to solve the problems we have long progress bar problem. And upload it to the original when all the temporary files into memory to cache temporary files when it reaches the set value is automatically saved to the hard disk, effectively improved memory utilization. Its role is to give in principle upload upload each time a unique ID, when the PHP script receives an upload file, explaining the program will automatically check for $_POST数组中 hidden field named APC_UPLOAD_PROGRESS, it will become variable caching, storing information about uploaded so that the script can access uploaded files by uploading a state ID information.
<!–以下为上传表单–>
<form enctype="multipart/form-data" id="upload_form" action="" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="upid"/>
视频标题:<input type="text" id="subject" name="subject"/>
视频说明:<input type="text" id="content" name="content"/>
视频TAG(以逗号分割)<input type="text" id="tag" name="tags"/>
<input type="file" id="upfile" name="upfile"/>
<input type="submit" id="filesubmit" value="上传" onclick="startProgress(‘upid’); return true;"/>
<!–注意:startProgress(‘upid’)中的参数是你从php中分配的唯一上传参数–>
</form>
<!–以下为上传进度条–>
<div id="upstatus" style="width: 500px; height: 30px; border: 1px solid ##ffffde; color:#796140;">
</div
<div id="progressouter" style="width: 500px; height: 20px; border: 3px solid #de7e00; display:none;">
<div id="progressinner" style="position: relative; height: 20px; color:#796140; background-color: #f6d095; width: 0%; "></div>
</div>
The most important is that APC_UPLOAD_PROGRESS hidden field, and with it access to the current state of the script to upload files, plus one extra div upload status display just fine. The following is a script processing Ajax, I used the Jquery framework, json deliver the message. f
unction getProgress(upid){
var url = "<{$siteurl}>epadmin/upprocess";
$.getJSON(
url,
{ progress_key: upid },
function(json){
$("#progressinner").width(json.per+"%");
$("#upstatus").html(‘文件大小:’+json.total+‘KB’+‘ 已上传:’+json.current+‘KB’);
if (json.per < 100){
setTimeout(function(){
getProgress(upid);
}, 10);
}else{
$("#upstatus").html("视频上传完成,正在处理数据,请稍后……");
}
}
)
}
function startProgress(upid){
$("#progressouter").css({ display:"block" });
setTimeout(function(){
getProgress(upid);
}, 100);
}
Further down is to read the upload status of PHP code, as to deal with file uploads can follow their own to write the usual // upload the file manipulation functions, you can write according to their 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);
 }
 }
 //这就是读取上传状态的函数了~~
 function 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);
 }
 }
Well, now you can deploy your own site, and see for yourself the effect is not that cool?

Reproduced in: https: //my.oschina.net/766/blog/211429

Guess you like

Origin blog.csdn.net/weixin_33813128/article/details/91546368