【发布时间】:2012-04-29 10:01:50
【问题描述】:
所以我尝试使用进度条进行上传,我安装了uploadprogress pecl,如果表单中的操作导致upload.php,任何其他名称,并且它停止工作,则上传工作完美。
如果名称不是upload.php,则输出只是“100”(可以在下面的getprogress.php文件中看到原因)
这是形式:(这个版本有效,因为文件是upload.php)
<form method="post" action="/test/upload.php" enctype="multipart/form-data" id="upload-form" target="upload-frame">
<input type="hidden" id="uid" name="UPLOAD_IDENTIFIER" value="<?php echo $uid; ?>">
<input type="file" name="file">
<input type="submit" name="submit" value="Upload!">
</form>
</div>
<div style="float:left;width:100%;">
<div id="progress-bar"></div>
</div>
<iframe id="upload-frame" name="upload-frame"></iframe>
这是 jquery:
<script>
(function ($) {
var pbar;
var started = false;
$(function () {
$('#upload-form').submit(function() {
pbar = $('#progress-bar');
pbar.show().progressbar();
$('#upload-frame').load(function () {
started = true;
});
setTimeout(function () {
updateProgress($('#uid').val());
}, 1000);
});
});
function updateProgress(id) {
var time = new Date().getTime();
$.get('../uploadprogress/getprogress.php', { uid: id, t: time }, function (data) {
var progress = parseInt(data, 10);
if (progress < 100 || !started) {
started = progress < 100;
updateProgress(id);
}
started && pbar.progressbar('value', progress);
});
}
}(jQuery));
</script>
这是文件getprogress.php
<?php
if (isset($_GET['uid'])) {
// Fetch the upload progress data
$status = uploadprogress_get_info($_GET['uid']);
if ($status) {
// Calculate the current percentage
echo round($status['bytes_uploaded']/$status['bytes_total']*100, 1);
}
else {
// If there is no data, assume it's done
echo 100;
}
}
?>
我花了大约 5 个小时试图找出原因,但我不能。非常感谢您的帮助。
【问题讨论】:
-
那么如果重命名文件
upload.php和表单中的upload.php值,根本就不行? -
不,简单地说,如果表单中的名称例如为
uploading.php,则getprogress.php的输出始终为100。如果是upload.php,则输出为实际上传的% 值. -
但是您实际上将文件从
upload.php重命名为uploading.php而不仅仅是更改表单值,对吧? -
上传过程中$status['bytes_uploaded']和$status['bytes_total']的值是多少?我猜他们中的一个(或两个)是错误的?哪一个?
标签: php jquery upload progress-bar pecl