<?php 
session_start
();

//Where to go after file upload is completed
$redirect "progress.php";

if(isset(
$_GET['progress_key'])) {
  
$status apc_fetch('upload_'.$_GET['progress_key']);
  echo 
json_encode($status);
  exit;
}

if(
$_SERVER['REQUEST_METHOD']=='POST') {
$status apc_fetch('upload_'.$_POST['APC_UPLOAD_PROGRESS']);


    
//upload the file to the server
        
if($_FILES['file'] && $_FILES['file']['size'] > 0){
            
$uploaddir "";  //set this to where your files should be uploaded.  Default is same directory as script.
            
$filename $_FILES['file']['name'];
            
$uploadfile $uploaddir $_FILES['file']['name'];
            
move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile);
            
$file $filename;
        }
    
//


    //make sure they entered a name for the file.  If so, continute...
    
if($_POST['name']) {
    
//Insert into database if applicable - or perform other actions here

//        mysql_query("INSERT INTO files (name, file, date) VALUES 
//            (
//            '".addslashes($_POST['name'])."',
//            '".$file."',
//            '".time()."')
//        ");
//    

    //set a session for confirmation message
    
$_SESSION['success'] = 1;
    }
    
            
$status['done'] = 1;
            echo 
json_encode($status);
            exit;
    
    
//
    
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>File Upload</title>
<script type="text/javascript" src="yui/yahoo.js"></script>
<script type="text/javascript" src="yui/event.js"></script>
<script type="text/javascript" src="yui/dom.js"></script>
<script type="text/javascript" src="yui/animation.js"></script>
<script type="text/javascript" src="yui/dragdrop.js"></script>
<script type="text/javascript" src="yui/connection.js"></script>
<script type="text/javascript" src="yui/container.js"></script>
<link rel="stylesheet" type="text/css" href="yui/container.css" />

<script type="text/javascript">
var fN = function callBack(o) {
  var resp = eval('(' + o.responseText + ')');
  var rate = parseInt(resp['rate']/1024);
  if(resp['cancel_upload']) {
    txt="Cancelled after "+resp['current']+" bytes!"; 
  } else {
    txt="";
  }
<!--  txt += "<br>Upload rate was "+rate+" kbps.";-->
  document.getElementById('pbar').style.width = '100%';
  document.getElementById('ppct').innerHTML = "100%";
  document.getElementById('ptxt').innerHTML = txt;
  setTimeout("progress_win.hide(); window.location.reload()",1000);
  window.location = "<?php echo $redirect?>"
}
var callback = { upload:fN }

var fP = function callBack(o) {
  var resp = eval('(' + o.responseText + ')');
  if(!resp['done']) { 
    if(resp['total']) {
      var pct = parseInt(100*(resp['current']/resp['total']));
      document.getElementById('pbar').style.width = ''+pct+'%';
      document.getElementById('ppct').innerHTML = " "+pct+"%";
<!--      document.getElementById('ptxt').innerHTML = resp['current']+" of "+resp['total']+" bytes";-->
    }
    setTimeout("update_progress()",500);
  } else if(resp['cancel_upload']) {
    txt="Cancelled after "+resp['current']+" bytes!"; 
    document.getElementById('ptxt').innerHTML = txt;
    setTimeout("progress_win.hide(); window.location.reload();",2000);
    window.location = "<?php echo $redirect?>"
  }
}
var progress_callback = { success:fP }

function update_progress() {
  progress_key = document.getElementById('progress_key').value;
  YAHOO.util.Connect.asyncRequest('GET','progress.php?progress_key='+progress_key, progress_callback);
}

var progress_win;

function postForm(target,formName) {
  YAHOO.util.Connect.setForm(formName,true);
  YAHOO.util.Connect.asyncRequest('POST',target,callback);
/* Is there some event that triggers on an aborted file upload? */
/*   YAHOO.util.Event.addListener(window, "abort", function () { alert('abort') } ); */

  progress_win = new YAHOO.widget.Panel("progress_win", { width:"420px", fixedcenter:true, underlay:"shadow", close:false, draggable:false, modal:true, effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.3} } );
  progress_win.setHeader("Uploading file...");
  progress_win.setBody('<div style="height: 1em; width: 400px; border:1px solid #000;"> <div id="pbar" style="background: #99e; height: 98%; width:0%; float:none;">&nbsp;</div> <div id="ppct" style="height: 90%; margin: 1 0 0 185;">0%</div></div><div id="ptxt" style="height:10px; margin: 3 0 0 5">  </div>');
  progress_win.render(document.body);
  update_progress();
}
</script>

<script type="text/javascript">
<!--hide the progress meter unless a file is chosen.
    function toggle_visibility(id) {
       var e = document.getElementById(id);
       if(e.style.display == 'block')
          e.style.display = 'none';
       else
          e.style.display = 'block';
    }
//-->
</script>


<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>


<div class="form-style">

<?php 
//Confirmation message
if (isset($_SESSION['success'])) { 

    echo 
"<span class='confirmation'>Your file has been uploaded.</span><br><br><br>";
    unset(
$_SESSION['success']);
}
//
?>

<form enctype="multipart/form-data" id="form1" name="form1" method="POST" action="" onsubmit="postForm('progress.php','form1'); return false;" >
  File Name <br />
  <input name="name" type="text" id="name" size="45" />
  <br />
  <br />
Description<br />
<textarea name="description" cols="55" rows="2" id="description"></textarea>
<br />
<br />
Choose a file <br />
<input type="hidden" name="APC_UPLOAD_PROGRESS" id="progress_key" value="<?php echo uniqid()?>"/>
<input name="file" onclick="toggle_visibility('progress_win');" type="file" id="file" />
<br />
<div id="progress_win" style="display:none">
  <div class="hd" style="color: #333333; background: #D3D3D3"></div>
  <div class="bd"></div>
  <div class="ft"></div>
</div>
<br />
<input type="submit" name="Submit" value="Submit" />
</form>

</div>

</body>
</html>