php 图片上传进度条代码方法

1.APC扩展库的安装(windows);

(1)首先下载php_apc.dll,然后再php.ini里配置:在扩展库后加(;extension=php_zip.dll)->

extension=php_apc.dll
apc.rfc1867=1
apc.cache_by_default    =Off   
apc.enable_cli       =Off   
apc.enabled       =On   
apc.file_update_protection =2   
apc.filters       =""
apc.gc_ttl       =3600
apc.max_file_size    =200M   
apc.num_files_hint    =1000
apc.optimization    =On   
apc.shm_segments    =1   
apc.shm_size       =30   
apc.slam_defense    =Off   
apc.stat       =On   
apc.ttl          =0   
apc.user_entries_hint    =100   
apc.user_ttl       =0

(2)把php_apc.dll复制到c盘windows文件夹下

(3)重启apache,测试有没安装成功!

if(function_exists("apc_fetch")){

echo "安装成功!";

}

else

{

echo "安装失败,请重新安装!";

}

2.把相关的代码贴上

(1)getprogress.php


<?php
session_start();
if(isset($_GET['progress_key'])) {
$status =apc_fetch('upload_'.$_GET['progress_key']);
echo ($status['current']/$status['total'])*100;
}
?>

(2)upload.php

<style type="text/css">
*{margin:0px;padding:0px;}
</style>
<?php
   $id = $_GET['id'];
   if($_GET["add"]=="ok")
   {
    echo "上传成功!";
   }
   else{
?>
<form enctype="multipart/form-data" id="upload_form" action="target.php" name="upload_form" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo $id?>"/>
<input type="file" id="test_file" name="test_file" οnchange="window.parent.startProgress();" style="width:190px;height:20px; background-color:gray;"/><br/>
</form>
<?php
}
?>

(3)progress.php

<?php
   $id = md5(uniqid(rand(), true));
?>
<html>
<head><title>php文件上传进度</title></head>
<body>
<script type="text/javascript">
var xmlHttp;
var proNum=0;
var loop=0;
var Try = {
these: function() {
    var returnValue;
    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      }
catch (e) {}
    }
    return returnValue;
}
}

function createXHR(){
return Try.these(
      function() {return new XMLHttpRequest()},
      function() {return new ActiveXObject('Msxml2.XMLHTTP')},
      function() {return new ActiveXObject('Microsoft.XMLHTTP')}
    ) || false;
}

var xmlHttp;

function sendURL() {
    xmlHttp=createXHR();
    var url="getprogress.php?progress_key=<?php echo $id;?>";
    xmlHttp.onreadystatechange=doHttpReadyStateChange;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);  
}
function doHttpReadyStateChange() {
   if (xmlHttp.readyState == 4){
proNum=parseInt(xmlHttp.responseText);
document.getElementById("progressinner").style.width = proNum+"%";
document.getElementById("showNum").innerHTML = proNum+"%";
if (proNum<100){
   setTimeout("getProgress()",500);
}
else
{
   window.theframe.location.href='upload.php?add=ok';
}
}
}

function getProgress(){
loop++;
document.getElementById("showNum2").innerHTML = loop;
sendURL();
}
function startProgress(){
    document.getElementById("progressouter").style.display="block";
    setTimeout("getProgress()",500);
window.theframe.upload_form.submit();
}
</script>
<iframe id="theframe" name="theframe" src="upload.php?id=<?php echo($id); ?>" style=" height:20px; width: 190px;" frameborder="0" scrolling="no">
</iframe>
<div id="progressouter" style="font-size:0px;width:300px;height:5px;border: 1px solid red;display:none;">
<div id="progressinner" style="font-size:0px;position: relative; height:5px; background-color: purple; width:0%;">
</div>
</div><div id='showNum'></div><br>
<div id='showNum2'></div>
</body>
</html>

(4)target.php

<?php
set_time_limit(600);
if($_SERVER['REQUEST_METHOD']=='POST') {
move_uploaded_file($_FILES["test_file"]["tmp_name"],dirname($_SERVER['SCRIPT_FILENAME'])."/upload/" . $_FILES["test_file"]["name"]);//UploadTemp文件夹位于此脚本相同目录下
}
?>

3.打开progress.php进行测试,可以把源码修改成更好的!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值