,继上次运用了uploadprogress上传扩展,自己弄了一个简单的实例,实现原理和apc实现的方法基本一样。
1.安装php_uploadprogress.dll扩展,重启apache
2.实现代码以下:
upload.php
PHP Code
复制内容到剪贴板
- <?php
- $id = $_GET['id'];
- ?>
- <form enctype="multipart/form-data" id="upload_form" action="target.php" method="POST">
- <input type="hidden" name="UPLOAD_IDENTIFIER" value="<?php echo $id;?>" />
- <input type="file" id="test_file" name="test_file"/><br/>
- <input οnclick="window.parent.startProgress(); return true;" type="submit" value="上传"/>
- </form>
target.php
PHP Code
复制内容到剪贴板
- <?php
- set_time_limit(600);
- if($_SERVER['REQUEST_METHOD']=='POST') {
- move_uploaded_file($_FILES["test_file"]["tmp_name"],
- dirname($_SERVER['SCRIPT_FILENAME'])."/UploadTemp/" . $_FILES["test_file"]["name"]);//UploadTemp文件夹位于此脚本相同目录下
- echo "<p>上传成功</p>";
- }
- ?>
getprogress.php
PHP Code
复制内容到剪贴板
- <?php
- if (function_exists("uploadprogress_get_info")) {
- $info = uploadprogress_get_info($_GET['progress_key']);
- if(!emptyempty($info)){
- if(($info['bytes_uploaded'] < $info['bytes_total']) && !emptyempty($info['bytes_uploaded']) && !emptyempty($info['bytes_total'])){
- $proNum = floor(($info['bytes_uploaded']/$info['bytes_total'])*100);
- }else{
- $proNum = 100;
- }
- echo $proNum;
- }else{
- }
- }
- ?>
progresstest.php
PHP Code
复制内容到剪贴板
- <?php
- $id = md5(microtime() . rand());
- ?>
- <html>
- <head><title>上传进度</title></head>
- <body>
- <script src="js/jquery-1.4.4.min.js" language="javascript"></script>
- <script language="javascript">
- var proNum=0;
- var loop=0;
- var progressResult = "";
- var interval;
- function sendURL() {
- $.ajax({
- type : 'GET',
- url : "getprogress.php",
- async : true,
- cache : false,
- dataType : 'json',
- data: "progress_key=<?php echo $id;?>",
- success : function(e) {
- proNum=parseInt(e);
- if(e){
- document.getElementById("progressinner").style.width = proNum+"%";
- document.getElementById("showNum").innerHTML = proNum+"%";
- setTimeout("getProgress()", 200);
- }else{
- if(interval == 1){
- document.getElementById("progressinner").style.width = "100%";
- document.getElementById("showNum").innerHTML = "100%";
- }
- }
- }
- });
- }
- function getProgress(){
- loop++;
- sendURL();
- }
- function startProgress(){
- interval = 1;
- document.getElementById("progressouter").style.display="block";
- document.getElementById("progressinner").style.width = proNum+"%";
- document.getElementById("showNum").innerHTML = proNum+"%";
- setTimeout("getProgress()", 500);
- }
- </script>
- <iframe id="theframe" name="theframe" src="upload.php?id=<?php echo $id; ?>"
- style="border: none; height: 100px; width: 400px;" >
- </iframe>
- <br/><br/>
- <div id="progressouter" style="width: 500px; height: 20px; border: 6px solid red; display:none;">
- <div id="progressinner" style="position: relative; height: 20px; background-color: purple; width: 0%; "></div>
- </div>
- <div id='showNum'></div><br>
- <div id='showNum2'></div>
- </body>
- </html>
同时可以下载demo uploadprogress.rar