Две вещи:
- Использование Javascript
- Использование APC
Javascript:
You can use plupload (http://www.plupload.com/)
- pretty good multiple file uploader
Pretty decent JS plugin for uploading files in PHP in two methods,
1. normal upload
2. chunk based uploading.
Ограничения / Допущения:
1. Flash Plugin - in the browser
2. Session problem - since it's flash, a different session's are created for
each file upload.
APC / PHP
Используя APC - вы можете создать индикатор выполнения самостоятельно.
Ограничение/ Допущения:
1. Install APC on the server - using PECL (pecl install APC)
2. Write a separate code to get the status of upload.
Код:
getprogress.php
<?php
if(isset($_GET['progress_key'])) {
$status = apc_fetch('upload_'.$_GET['progress_key']);
echo $status['current']/$status['total']*100;
}
?>
upload.php
<?php
$id = uniqid("");
?>
<html>
<head><title>Upload Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
function getProgress(){
$.get("getprogress.php?progress_key=<?php echo($id)?>",
function(percent) {
document.getElementById("progressinner").style.width = percent+"%";
if (percent < 100){
setTimeout("getProgress()", 100);
}
});
}
function startProgress(){
document.getElementById("progressouter").style.display="block";
setTimeout("getProgress()", 1000);
}
</script>
<iframe id="theframe" name="theframe"
src="fileupload.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>
</body>
</html>
fileupload.php
<?php
$id = $_GET['id'];
?>
<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?>"/>
<input type="file" id="test_file" name="test_file"/><br/>
<input onclick="window.parent.startProgress(); return true;"
type="submit" value="Upload!"/>
</form>