PHP using APC module to achieve large file upload progress bar Method _php Skill

Source: Internet
Author: User
Tags apc apc module file upload file upload progress bar rar

PHP large file with the progress of the upload, has been a PHP programmer very distressed problem. Query Baidu, Google, generally do with the progress of the upload method: flash+php,socket,apc+php, and so on, I introduced the APC +php+ajax production with the progress of the upload , and posted the source code, hope for everyone useful.
Alternative PHP cache (APC) is a free and publicly optimized code cache for PHP. It is used to provide a free, open and robust architecture to cache and optimize PHP's intermediate code.

When you use APC, you must first use the Install APC module.
The first step: Download Php_apc.dll

Step two: Let php.ini support the APC extension module.
put the Php_apc.dll in your ext directory and open php.ini to join:
Extension=php_apc.dll
apc.rfc1867 = On
Apc.max_file_size = 100M
Upload_max_filesize = 100M
Post_max_size = 100M
//The above parameters can be defined by themselves

Step three: Check to see if the PHP APC is supported

 if (function_exists (' Apc_fetch ')) {
 echo ' it surpport APC model! ';
 } else {
 echo "it ' s not support APC model!" ;
 }
 ? >

Let's get down to business:
principle: through the APC module, use Ajas to read the upload progress from the cache. See:
index.php

<?php $unid =uniqid ("");//Identify the only Peugeot, achieve multiple uploads at the same time?> <div class= "Userinput2" > <div id= "Captions" > First upload the software you want to upload the server, please wait while uploading ... <span class= "Style1" ><br/> </span> <script type= "Text/javascript"
 > var xmlHttp;
 var pronum=0;
 var loop=0;
 Initializes the XmlHttp function Createxml () {var xmlHttp; if (window.
 XMLHttpRequest) {xmlhttp=new XMLHttpRequest ();
 }else{xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");
 return xmlHttp;
 } xmlhttp=createxml (); Ajas operation function Sendurl () {var url= "getprogress.php?progress_key=<?php Echo $unid;?
 > ";
 Xmlhttp.open ("Get", url,false); if (Window.navigator.userAgent.indexOf ("Firefox") >=1) {//If firefox3.0 xmlhttp.send ("progress_key=<?php Echo $unid;?
 > ");
 
 if (xmlhttp.status==200) Dohttpreadystatechange ();
 }else{xmlhttp.onreadystatechange = Dohttpreadystatechange; Xmlhttp.send ("progress_key=<?php echo $unid;?
 > "); }//Callback Functions function Dohttpreadystatechange () {if (xmlhttp.readystate== 4) {
 Pronum=parseint (Xmlhttp.responsetext);
 alert (pronum);
 Document.getelementbyidx_x ("Progressinner"). Style.width = pronum+ "%";
 Document.getelementbyidx_x ("Shownum"). InnerHTML = pronum+ "%";
 if (Pronum < MB) {settimeout ("Sendurl ()", 200); }else{//uploaded successfully, the information can not be obtained in time.
 Also hope that high man pointing document.getelementbyidx_x ("Progressouter"). style.display= "None";
 Document.getelementbyidx_x ("Progressinner"). style.display= "None";
 Document.getelementbyidx_x ("Shownum"). style.display= "None";
 Document.getelementbyidx_x ("Theframe"). style.display= "None";
 Document.getelementbyidx_x ("Link2"). style.display= "Block";
 }} function Startprogress () {document.getelementbyidx_x ("Progressouter"). style.display= "Block";
 SetTimeout ("Sendurl ()", 200);
 function Newsofturl (text) {document.getelementbyidx_x ("Link2"). style.display= "Block";
 Document.getelementbyidx_x ("Link2"). Value=text; } </script> <iframe id= "theframe" name= theframe "src=" softupload.php?id=<?php Echo ($unid);?> "Style=" Border:0; height:80px; width:400px; "frameborder=" 0 "scrolling=" no "> </iframe> <input name=" Linkdefult "type=" hidden "id=" Linkdefult "value=" 0 "/> <br/> <div id=" link2 "style=" Display:none; "> <font size=2> Upload success!    file size is: <in Put type= "text" name= "FileSize" id= "FileSize" style= "width:50px"; /> </font><br> <br> <font size=2> file download address: </font><br/> <input type=text Name = ' link ' id= ' style= ' width:380px '/> </div> <br/> <div id= ' progressouter ' style= ' width:500px; height:20px; border:1px solid #000000; Display:none; " > <div id= "Progressinner" style= "position:relative; height:20px; Background-color: #333333; width:0%; "></div> </div> <div id= ' shownum ' style=" FONT-SIZE:12PX;
 

 Color: #333333 "></div> </div> </div>

softupload.php

<?php $id = $_get[' id '];?> <script language= "JavaScript" >//trim the input text function Trim (input) {var
 LRE =/^\s*/;
 var rre =/\s*$/;
 input = Input.replace (Lre, "");
 input = Input.replace (Rre, "");
 return input;
 function Checkfortestfile () {var file = document.getelementbyidx_x (' Softfile '); 
 var Filename=file.value;
 Checking for file browsed or not if (Trim (fileName) = = ') {alert ("Please select a file for upload!!!");
 File.focus ();
 return false; 
 //setting the extension array for diff. Type of text files var extarray = new Array (". rar", ". zip", ". exe", ". gz");

 Getting the file name while (Filename.indexof ("\")!=-1) fileName = Filename.slice (Filename.indexof ("\") + 1); Getting the file extension var ext = Filename.slice (Filename.indexof (".")).
 toLowerCase ();
 for (var i = 0; i < extarray.length i++) {if (extarray[i] = ext) {window.parent.startProgress (); Alert ("Correct file format is" + (Extarray.join ("")) + \ n Please select a new "+" file submit upload.");
 File.focus (); 
 return false; } </script> <meta http-equiv= "Content-type" content= "text/html;
charset=gb2312 "/> <form enctype=" Multipart/form-data "id=" Upload_form "action=" target.php "method=" POST "> <input type= "hidden" name= "apc_upload_progress" id= "Progress_key" value= "<?php echo $id?>"/> <table Width= "322" border= "0" cellpadding= "0" cellspacing= "0" id= "linktable" > <tr> <td >1. Select Software <br/> T;input name= "Softfile" type= "file" id= "Softfile"/></td> "<td ><br/> <input name=" Submit "type=

 "Submit" onclick= "return Checkfortestfile ();" value= "Upload Software"/></td> </tr> </table> </form>

target.php

<script language= "JavaScript" >//Returns the uploaded information to the parent window function Chuanzhi () {parent.document.getElementByIdx_x ('
FileSize '). value=document.getelementbyidx_x (' size '). Value;
parent.document.getElementByIdx_x (' link '). value=document.getelementbyidx_x (' Newsoftdir '). Value;
parent.document.getElementByIdx_x (' Linkdefult '). value=1; } </script> <body onload= "Chuanzhi ();"
> <?php//header (' content-type:text/html;charset=gb2312 '); Define (' Softdir ', "./upload/"); Post-upload path define (' Httpsoftdir ', "http://www.mysite.com/");
 Server's path//To determine whether the upload software suffix name allows function issoftext ($extension) {$ext = array (' exe ', ' rar ', ' zip ', ' gz '); Return In_array ($extension, $ext)?
True:false; } if ($_server[' Request_method ']== ' POST ') {$errors [' 0 '] = true; $errors [' 1 '] = ' Please select uploaded software picture '; $errors [' 2 '] = ' upload software picture failed '; $e
rrors[' 3 '] = ' upload software picture failed ';
$daytime = Date (' Y-m-d-h-m-s '); $timename =str_replace ("-", "", $daytime); Get the day's date//check whether the software is normal uploaded if (!is_uploaded_file ($_files[' softfile '] [' tmp_name '])) {echo <script> alert (' Abnormal upload!
 '); History.back ();</script> ";
 Exit
 $extension = PathInfo ($_files[' softfile '] [' name '], pathinfo_extension); $filename = $timename. "
 _ ". $_files[' softfile ' [' name '];
 $tmpsize =$_files[' softfile ' [' Size ']; $msize =round ($tmpsize/1048576, 2). "
 M "; $ksize =round ($tmpsize/1024, 2).
 "K";
 $filesize = $tmpsize >1048576 $msize: $ksize; Check the Software file format if (!issoftext ($extension)) {echo "<script>alert (' uploaded software is malformed!
 '); History.back ();</script> ";
 Exit }//Mobile software if (!move_uploaded_file ($_files[' softfile '] [' tmp_name '], Softdir. $filename)) {echo "<script>alert (' move Software Error!
 '); History.back ();</script> ";
 Exit }else{echo "<font size=2> upload success!    file size is: <input type=text id= ' size ' value= ' $filesize ' ></font&
 Gt;<br> "; echo "<font size=2> File Download address is: </font><input type=text id= ' Newsoftdir '".
 Httpsoftdir. $filename. "' style= ' width=380 ' > '; }else echo "Please do not enter the address directly!"

";

 ?>

getprogress.php

<?php
//Upload Ajas Get Progress page
session_start ();
if (Isset ($_get[' Progress_key ')) {
 $status = apc_fetch (' upload_ '. $_get[' Progress_key ']);
 Echo ($status [']/$status [' Total ']) *100
}
Echo ' apc_file= '. Apc_file;
? >

This article provides you with a PHP production with progress upload file ideas, there may be some lack of place, I hope that we can supplement, or combined with small series before finishing the article to learn, I hope 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.