The upload of php large files with progress has always been a problem that makes php programmers very troubled. Query baidu and Google. The general upload methods for providing progress are: flash+php, socket, apc+php, etc. I will introduce below.Upload with progress made by apc +php + ajax, and post the source code, hoping it will be useful to everyone.
Alternative PHP Cache (APC) is a free and publicly optimized code cache for PHP. It is used to provide a free, public and robust architecture to cache and optimize PHP's intermediate code.
When using apc, you must first use the installation of the apc module.
Step 1: Download php_apc.dll
Step 2: Let the apc extension module be supported.
Put php_apc.dll into your ext directory and open 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 yourself
Step 3: Check whether 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 to the topic:
principle:Through the APC module, use ajas to read the upload progress from the cache. See:
<?php $unid=uniqid("");//Determine the unique Peugeot and realize the upload of multiple people simultaneously?> <div class="userinput2"> <div >First upload the software you want to upload to the server,Please wait patiently when uploading...<span class="style1"><br /> </span> <script type="text/javascript" > var xmlHttp; var proNum=0; var loop=0; //Initialize xmlHttp function createxml(){ var xmlHttp; if(){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject(""); } return xmlHttp; } xmlHttp=createxml(); //ajas operation function sendURL() { var url="?progress_key=<?php echo $unid;?>"; ("GET",url,false); if (("Firefox")>=1){ //If it is firefox3.0 ("progress_key=<?php echo $unid;?>"); if(==200) doHttpReadyStateChange(); }else{ = doHttpReadyStateChange; ("progress_key=<?php echo $unid;?>"); } } //Callback function function doHttpReadyStateChange() { if (== 4){ proNum=parseInt(); //alert(proNum); document.getElementByIdx_x("progressinner"). = proNum+"%"; document.getElementByIdx_x("showNum").innerHTML = proNum+"%"; if ( proNum < 100){ setTimeout("sendURL()", 200); }else{ //After uploading is successful, information cannot be obtained in time. I hope experts can give me some advice document.getElementByIdx_x("progressouter").="none"; document.getElementByIdx_x("progressinner").="none"; document.getElementByIdx_x("showNum").="none"; document.getElementByIdx_x("theframe").="none"; document.getElementByIdx_x("link2").="block"; } } } function startProgress(){ document.getElementByIdx_x("progressouter").="block"; setTimeout("sendURL()", 200); } function newsofturl(text){ document.getElementByIdx_x("link2").="block"; document.getElementByIdx_x("link2").value=text; } </script> <iframe name="theframe" src="?id=<?php echo($unid); ?>" style="border: 0; height: 80px; width: 400px; " frameborder="0" scrolling="no" > </iframe> <input name="linkdefult" type="hidden" value="0" /> <br /> <div style="display:none;" > <font size=2>Upload successfully!&nbsp; &nbsp; The file size is: <input type="text" name="filesize" style="width:50px;"/> </font><br> <br> <font size=2>The file download address is:</font><br /> <input type=text name='link' id='link' style='width:380px;' /> </div> <br/> <div style="width: 500px; height: 20px; border: 1px solid #000000; display:none;"> <div style="position: relative; height: 20px; background-color: #333333; width: 0%; "></div> </div> <div id='showNum' style="font-size:12px; color:#333333"></div> </div> </div>
<?php $id = $_GET['id']; ?> <script language="javascript"> //Trim the input text function Trim(input) { var lre = /^\s*/; var rre = /\s*$/; input = (lre, ""); input = (rre, ""); return input; } function CheckForTestFile() { var file = document.getElementByIdx_x('Softfile'); var fileName=; //Checking for file browsed or not if (Trim(fileName) =='' ) { alert("Please select a file for upload!!!"); (); 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 (("\") != -1) fileName = (("\") + 1); //Getting the file extension var ext = ((".")).toLowerCase(); for (var i = 0; i < ; i++) { if (extArray[i] == ext) { (); return true; } } alert("The correct file format is" + ((" ")) + "\nPlease select a new one" + "File submission upload."); (); return false; } </script> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <form enctype="multipart/form-data" action="" method="POST"> <input type="hidden" name="APC_UPLOAD_PROGRESS" value="<?php echo $id?>"/> <table width="322" border="0" cellpadding="0" cellspacing="0" > <tr> <td >1.Select software<br /> <input name="Softfile" type="file" /></td> <td ><br /> <input name="submit" type="submit" onclick="return CheckForTestFile();" value="Upload software"/></td> </tr> </table> </form>
<script language="javascript"> //Return the uploaded information to the parent windowfunction chuanzhi(){ .getElementByIdx_x('filesize').value=document.getElementByIdx_x('size').value; .getElementByIdx_x('link').value=document.getElementByIdx_x('newsoftdir').value; .getElementByIdx_x('linkdefult').value=1; } </script> <body onLoad="chuanzhi();"> <?php //header('Content-Type:text/html;charset=gb2312'); define('SOFTDIR', "./upload/"); //Path after uploaddefine('HTTPSOFTDIR', "/"); //The path of the server //Discern whether the suffix name of the upload software is allowedfunction 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 the uploaded software picture'; $errors['2'] = 'Uploading software pictures failed'; $errors['3'] = 'Uploading software pictures failed'; $daytime = date('Y-m-d-h-m-s'); $timename=str_replace("-","",$daytime); //Get the date and time of the day //Check whether the software is uploaded normally if(!is_uploaded_file($_FILES['Softfile']['tmp_name'])) { echo "<script>alert('Unormal upload! ');();</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('The uploaded software format is incorrect! ');();</script>"; exit; } //Mobile software if(!move_uploaded_file($_FILES['Softfile']['tmp_name'], SOFTDIR. $filename)) { echo "<script>alert('The mobile software error! ');();</script>"; exit; }else{ echo "<font size=2>Upload successfully!&nbsp; &nbsp; The file size is:<input type=text id='size' value='$filesize'></font><br>"; echo "<font size=2>The file download address is:</font><input type=text id='newsoftdir' value='".HTTPSOFTDIR.$filename."' style='width=380'>"; } }else echo "Please don't enter the address directly!"; ?>
<?php //Upload ajas to get progress pagesession_start(); if(isset($_GET['progress_key'])) { $status = apc_fetch('upload_'.$_GET['progress_key']); echo ($status['current']/$status['total'])*100; } echo 'APC_FILE='.APC_FILE; ?>