php製作帶進度上傳文件的思路
本篇文章主要介紹php製作帶進度上傳文件的思路,有興趣的朋友參考下,希望對大家有幫助。
php 大檔案帶進度的上傳,一直是個令php程式設計師很苦惱的問題。查詢baidu 、Google ,大體做帶進度的上傳方式為:flash php,socket,apc php等,下面我介紹了apc php ajax製作的帶進度的上傳,並貼出源碼,希望對大家有用。
Alternative PHP Cache(APC)是 PHP 的一個免費公開的最佳化程式碼快取。它用來提供免費,公開且強健的架構來快取和最佳化 PHP 的中間程式碼。
在使用apc時候,先必須使用安裝apc 模組。
第一步:下載php_apc.dll
#第二步:讓php.ini支援apc擴充模組。 將php_apc.dll放入你的ext目錄,然後打開php.ini 加入:
extension=php_apc.dll
apc.rfc1867 = on# # .max_file_size = 100M
upload_max_filesize = 100M
post_max_size = 100M
# //以上參數可為自己定義為「以上參數」可定義為「以上參數」。 :檢查是否支援PHP APC
if (function_exists('apc_fetch')) { echo 'it surpport apc model!'; } else { echo "it's not support apc model!"; } ?>
下面進入正題:原理:
透過APC 模組,用ajas從快取中讀取上傳的進度。詳見: index.php <?php
$unid=uniqid("");//确定唯一标致,实现多人同时上传
?>
<p class="userinput2">
<p id="captions">先将你要上传的软件上传服务器,上传时请耐心等候...<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;?>");
}
}
//回调函数
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{
//上传成功后,还不能及时得到信息。还希望高人指点
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 />
<p 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;' />
</p>
<br/>
<p id="progressouter" style="width: 500px; height: 20px; border: 1px solid #000000; display:none;">
<p id="progressinner" style="position: relative; height: 20px; background-color: #333333; width: 0%; "></p>
</p>
<p id='showNum' style="font-size:12px; color:#333333"></p>
</p>
</p>
<?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("请为上传选择一个文件!!!"); 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请选择一个新的 " + "文件提交上传."); 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>
<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/"); //服务器的路径 //判断上传软件后缀名是否允许 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'] = '请选择上传的软件图片'; $errors['2'] = '上传软件图片失败'; $errors['3'] = '上传软件图片失败'; $daytime = date('Y-m-d-h-m-s'); $timename=str_replace("-","",$daytime); //取得当天的日期时间 //检查软件是否是正常上传的 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('上传的软件格式有错误!');history.back();</script>"; exit; } //移动软件 if(!move_uploaded_file($_FILES['Softfile']['tmp_name'], SOFTDIR. $filename)) { echo "<script>alert('移动软件出错!');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 "请不要直接输入地址!"; ?>
<?php //上传ajas获取进度页面 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; ?>
以上是php製作帶進度上傳文件的思路的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP在電子商務、內容管理系統和API開發中廣泛應用。 1)電子商務:用於購物車功能和支付處理。 2)內容管理系統:用於動態內容生成和用戶管理。 3)API開發:用於RESTfulAPI開發和API安全性。通過性能優化和最佳實踐,PHP應用的效率和可維護性得以提升。

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP仍然具有活力,其在現代編程領域中依然佔據重要地位。 1)PHP的簡單易學和強大社區支持使其在Web開發中廣泛應用;2)其靈活性和穩定性使其在處理Web表單、數據庫操作和文件處理等方面表現出色;3)PHP不斷進化和優化,適用於初學者和經驗豐富的開發者。

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP和Python各有優劣,選擇取決於項目需求和個人偏好。 1.PHP適合快速開發和維護大型Web應用。 2.Python在數據科學和機器學習領域佔據主導地位。

PHP主要是過程式編程,但也支持面向對象編程(OOP);Python支持多種範式,包括OOP、函數式和過程式編程。 PHP適合web開發,Python適用於多種應用,如數據分析和機器學習。
