欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP利用APC模塊實(shí)現(xiàn)大文件上傳進(jìn)度條的方法

 更新時(shí)間:2015年10月29日 17:11:02   作者:西門苦行僧  
這篇文章主要介紹了PHP利用APC模塊實(shí)現(xiàn)大文件上傳進(jìn)度條的方法,并分享了相關(guān)配置說明,感興趣的朋友可以學(xué)習(xí)借鑒。

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>上傳成功!&nbsp; &nbsp; 文件大小為:
 <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>上傳成功!&nbsp; &nbsp; 文件大小為:<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)文章

  • php 冒泡排序 交換排序法

    php 冒泡排序 交換排序法

    冒泡排序-php版本-交換排序法,需要的朋友可以參考下。
    2011-05-05
  • Zend Studio (eclipse)使用速度優(yōu)化方法

    Zend Studio (eclipse)使用速度優(yōu)化方法

    Zend studio7.12那速度正太讓人火大了,修改文件的保存就building workspace,要得等上好一會(huì)
    2011-03-03
  • 使用PHP socke 向指定頁面提交數(shù)據(jù)

    使用PHP socke 向指定頁面提交數(shù)據(jù)

    一直以為有了ajax別人網(wǎng)站的數(shù)據(jù)就可以拿過來用,這也是我這幾天想的一個(gè)方像,但是用了firefox測(cè)試之后,現(xiàn)在不能,
    2008-07-07
  • PHP實(shí)現(xiàn)的簡單異常處理類示例

    PHP實(shí)現(xiàn)的簡單異常處理類示例

    這篇文章主要介紹了PHP實(shí)現(xiàn)的簡單異常處理類,結(jié)合具體實(shí)例形式分析了php基于面向?qū)ο蠹夹g(shù)實(shí)現(xiàn)異常處理操作的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-05-05
  • PHP準(zhǔn)確取得服務(wù)器IP地址的方法

    PHP準(zhǔn)確取得服務(wù)器IP地址的方法

    這篇文章主要介紹了PHP準(zhǔn)確取得服務(wù)器IP地址的方法,可實(shí)現(xiàn)跳過代理直接獲取IP的功能,實(shí)例分析了php獲取服務(wù)器IP地址的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • 詳解Swoole TCP流數(shù)據(jù)邊界問題解決方案

    詳解Swoole TCP流數(shù)據(jù)邊界問題解決方案

    本文主要介紹了Swoole TCP流數(shù)據(jù)邊界問題解決方案,對(duì)Swoole感興趣的同學(xué),可以參考下。
    2021-05-05
  • PHP反序列化字符串逃逸實(shí)例詳解

    PHP反序列化字符串逃逸實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于PHP反序列化字符串逃逸的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • php 錯(cuò)誤處理經(jīng)驗(yàn)分享

    php 錯(cuò)誤處理經(jīng)驗(yàn)分享

    在創(chuàng)建腳本和 web 應(yīng)用程序時(shí),錯(cuò)誤處理是一個(gè)重要的部分。如果您的代碼缺少錯(cuò)誤檢測(cè)編碼,那么程序看上去很不專業(yè),也為安全風(fēng)險(xiǎn)敞開了大門
    2011-10-10
  • php用ini_get獲取php.ini里變量值的方法

    php用ini_get獲取php.ini里變量值的方法

    這篇文章主要介紹了php用ini_get獲取php.ini里變量值的方法,實(shí)例分析了ini_get函數(shù)的使用技巧,需要的朋友可以參考下
    2015-03-03
  • PHP實(shí)現(xiàn)限制域名訪問的實(shí)現(xiàn)代碼(本地驗(yàn)證)

    PHP實(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

最新評(píng)論