要监视上传进度,不外乎两种方案
1. 在客户端监测发送了多少数据
2. 在服务端监测接收了多少数据
APC是在服务端方案的一种。首先是Windows下APC的配置:
[PHP_APC]
extension=php_apc.dll
apc.rfc1867 = on
然后是测试代码:
upload.php
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$raw = file_get_contents($_FILES['file']['tmp_name']);
echo (int)(strlen($raw) / 1024 / 1024) . ' MB';
exit;
}
session_start();
$_SESSION['id'] = uniqid();
?>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="APC_UPLOAD_PROGRESS" value="<?php echo $_SESSION['id'] ?>" />
<input type="file" name="file" />
<input type="submit"/>
</form>
progress.php
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$status = apc_fetch('upload_'.$_SESSION['id']);
var_dump($_SESSION['id']);
echo json_encode($status);
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Upload Progress Moniter</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
window.setInterval(function() {
$.post('progress.php', function(data) {
$('#msg').text(data);
})
}, 100);
</script>
</head>
<body>
<pre id="msg"></pre>
</body>
</html>
此方法对于SAE无效,因为请求会被Balance到不同的主机,而APC的信息存在于“本地”,而非所有电脑都一样!