PHP利用APC模塊實(shí)現(xiàn)大文件上傳進(jìn)度條的方法
php 大文件帶進(jìn)度的上傳,一直是一個(gè)令php程序員很苦惱的問題。查詢baidu 、Google ,大體做帶進(jìn)度的上傳方式為:flash+php,socket,apc+php等,下面我介紹了apc +php+ajax制作的帶進(jìn)度的上傳,并貼出源碼,希望對(duì)大家有用。
Alternative PHP Cache(APC)是 PHP 的一個(gè)免費(fèi)公開的優(yōu)化代碼緩存。它用來提供免費(fèi),公開并且強(qiáng)健的架構(gòu)來緩存和優(yōu)化 PHP 的中間代碼。
在使用apc時(shí)候,先必須使用安裝apc 模塊。
第一步:下載php_apc.dll
第二步:讓php.ini支持apc擴(kuò)展模塊。
將php_apc.dll放入你的ext目錄,然后打開php.ini 加入:
extension=php_apc.dll
apc.rfc1867 = on
apc.max_file_size = 100M
upload_max_filesize = 100M
post_max_size = 100M
//以上參數(shù)可自己定義
第三步:檢查是否支持PHP APC
if (function_exists('apc_fetch')) { echo 'it surpport apc model!'; } else { echo "it's not support apc model!"; } ?>
下面進(jìn)入正題:
原理:通過APC 模塊,用ajas從緩存中讀取上傳的進(jìn)度。詳見:
index.php
<?php $unid=uniqid("");//確定唯一標(biāo)致,實(shí)現(xiàn)多人同時(shí)上傳 ?> <div class="userinput2"> <div id="captions">先將你要上傳的軟件上傳服務(wù)器,上傳時(shí)請(qǐng)耐心等候...<span class="style1"><br /> </span> <script type="text/javascript" > var xmlHttp; var proNum=0; var loop=0; //初始化xmlHttp function createxml(){ var xmlHttp; if(window.XMLHttpRequest){ xmlHttp=new XMLHttpRequest(); }else{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } return xmlHttp; } xmlHttp=createxml(); //ajas操作 function sendURL() { var url="getprogress.php?progress_key=<?php echo $unid;?>"; xmlHttp.open("GET",url,false); if (window.navigator.userAgent.indexOf("Firefox")>=1){ //如果是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;?>"); } } //回調(diào)函數(shù) 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 < 100){ setTimeout("sendURL()", 200); }else{ //上傳成功后,還不能及時(shí)得到信息。還希望高人指點(diǎn) 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>上傳成功! 文件大小為: <input type="text" name="filesize" id="filesize" style="width:50px;"/> </font><br> <br> <font size=2>文件下載地址為:</font><br /> <input type=text name='link' id='link' 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("請(qǐng)為上傳選擇一個(gè)文件!!!"); 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(); return true; } } alert("正確的文件格式為" + (extArray.join(" ")) + "\n請(qǐng)選擇一個(gè)新的 " + "文件提交上傳."); 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.選擇軟件<br /> <input name="Softfile" type="file" id="Softfile" /></td> <td ><br /> <input name="submit" type="submit" onclick="return CheckForTestFile();" value="上傳軟件"/></td> </tr> </table> </form>
target.php
<script language="javascript"> //將上傳后的信息返還給父窗口 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/"); //上傳后路徑 define('HTTPSOFTDIR', "http://www.mysite.com/"); //服務(wù)器的路徑 //判斷上傳軟件后綴名是否允許 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'] = '請(qǐng)選擇上傳的軟件圖片'; $errors['2'] = '上傳軟件圖片失敗'; $errors['3'] = '上傳軟件圖片失敗'; $daytime = date('Y-m-d-h-m-s'); $timename=str_replace("-","",$daytime); //取得當(dāng)天的日期時(shí)間 //檢查軟件是否是正常上傳的 if(!is_uploaded_file($_FILES['Softfile']['tmp_name'])) { echo "<script>alert('非正常上傳!');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; //檢查軟件文件格式 if(!isSoftExt($extension)) { echo "<script>alert('上傳的軟件格式有錯(cuò)誤!');history.back();</script>"; exit; } //移動(dòng)軟件 if(!move_uploaded_file($_FILES['Softfile']['tmp_name'], SOFTDIR. $filename)) { echo "<script>alert('移動(dòng)軟件出錯(cuò)!');history.back();</script>"; exit; }else{ echo "<font size=2>上傳成功! 文件大小為:<input type=text id='size' value='$filesize'></font><br>"; echo "<font size=2>文件下載地址為:</font><input type=text id='newsoftdir' value='".HTTPSOFTDIR.$filename."' style='width=380'>"; } }else echo "請(qǐng)不要直接輸入地址!"; ?>
getprogress.php
<?php //上傳ajas獲取進(jìn)度頁面 session_start(); if(isset($_GET['progress_key'])) { $status = apc_fetch('upload_'.$_GET['progress_key']); echo ($status['current']/$status['total'])*100; } echo 'APC_FILE='.APC_FILE; ?>
本文為大家提供了一個(gè)php制作帶進(jìn)度上傳文件的思路,可能還有一些欠缺的地方,希望大家進(jìn)行補(bǔ)充,或者是再結(jié)合小編之前整理的文章進(jìn)行學(xué)習(xí),希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
Zend Studio (eclipse)使用速度優(yōu)化方法
Zend studio7.12那速度正太讓人火大了,修改文件的保存就building workspace,要得等上好一會(huì)2011-03-03使用PHP socke 向指定頁面提交數(shù)據(jù)
一直以為有了ajax別人網(wǎng)站的數(shù)據(jù)就可以拿過來用,這也是我這幾天想的一個(gè)方像,但是用了firefox測(cè)試之后,現(xiàn)在不能,2008-07-07詳解Swoole TCP流數(shù)據(jù)邊界問題解決方案
本文主要介紹了Swoole TCP流數(shù)據(jù)邊界問題解決方案,對(duì)Swoole感興趣的同學(xué),可以參考下。2021-05-05PHP實(shí)現(xiàn)限制域名訪問的實(shí)現(xiàn)代碼(本地驗(yàn)證)
這篇文章主要介紹了PHP實(shí)現(xiàn)限制域名訪問的實(shí)現(xiàn)代碼,如果不是允許的域名不讓使用,然后讓客戶購買認(rèn)證即可,但本地驗(yàn)證加密一般都可以被破解,建議用遠(yuǎn)程驗(yàn)證,都收費(fèi)了不差這點(diǎn)錢了,需要的朋友可以參考下2020-09-09