《PHP實戰:PHP利用APC模塊實現大文件上傳進度條的方法》要點:
本文介紹了PHP實戰:PHP利用APC模塊實現大文件上傳進度條的方法,希望對您有用。如果有疑問,可以聯系我們。
PHP實戰php 大文件帶進度的上傳,一直是一個令php程序員很苦惱的問題.查詢baidu 、Google ,大體做帶進度的上傳方式為:flash+php,socket,apc+php等,下面我介紹了apc +php+ajax制作的帶進度的上傳,并貼出源碼,希望對大家有用.
Alternative PHP Cache(APC)是 PHP 的一個免費公開的優化代碼緩存.它用來提供免費,公開并且強健的架構來緩存和優化 PHP 的中間代碼.?
PHP實戰在使用apc時候,先必須使用安裝apc 模塊.
第一步:下載php_apc.dll
PHP實戰第二步:讓php.ini支持apc擴展模塊.
將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
???? //以上參數可自己定義?
PHP實戰第三步:檢查是否支持PHP APC
PHP實戰
if (function_exists('apc_fetch')) {
echo 'it surpport apc model!';
} else {
echo "it's not support apc model!";
}
?>
PHP實戰下面進入正題:
原理:通過APC 模塊,用ajas從緩存中讀取上傳的進度.詳見:
?index.php?
PHP實戰
<?php
$unid=uniqid("");//確定唯一標致,實現多人同時上傳
?>
<div class="userinput2">
<div 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 />
<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>
PHP實戰softupload.php
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("請為上傳選擇一個文件!!!");
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>
PHP實戰target.php
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/"); //服務器的路徑
//判斷上傳軟件后綴名是否允許
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實戰getprogress.php
PHP實戰
<?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制作帶進度上傳文件的思路,可能還有一些欠缺的地方,希望大家進行補充,或者是再結合小編之前整理的文章進行學習,希望對大家的學習有所贊助.
維易PHP培訓學院每天發布《PHP實戰:PHP利用APC模塊實現大文件上傳進度條的方法》等實戰技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培養人才。
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/8489.html