您好,登錄后才能下訂單哦!
怎么在PHP中使用APC模塊上傳大文件?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
第一步:下載php_apc.dll
第二步:讓php.ini支持apc擴(kuò)展模塊。
將php_apc.dll放入你的ext目錄,然后打開(kāi)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)入正題:
原理:通過(guò)APC 模塊,用ajas從緩存中讀取上傳的進(jìn)度。詳見(jià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); ?>" frameborder="0" scrolling="no" > </iframe> <input name="linkdefult" type="hidden" id="linkdefult" value="0" /> <br /> <div id="link2" > <font size=2>上傳成功! 文件大小為: <input type="text" name="filesize" id="filesize" /> </font><br> <br> <font size=2>文件下載地址為:</font><br /> <input type=text name='link' id='link' style='width:380px;' /> </div> <br/> <div id="progressouter" > <div id="progressinner" ></div> </div> <div id='showNum' ></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)度頁(yè)面 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; ?>
關(guān)于怎么在PHP中使用APC模塊上傳大文件問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。