<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="uploaded" type="file" />
<input type="submit" value="Upload" />
</form>
<?php
if(isset($_REQUEST['submit'])){
$target = "data/".basename( $_FILES['uploaded']['name']) ;
move_uploaded_file($_FILES['uploaded']['tmp_name'], $target);
}
?>
我非常了解Javascript,AJAX和JQuery等,我相信可以使用PHP,AJAX和Javascript等创建上传进度条。
我很惊讶如何获得上传的大小(意味着我想知道的每一秒,上传了多少文件以及剩余多少,我认为应该可以使用AJAX等)文件上传正在进行中。
这是PHP手册的链接,但我不明白: http://php.net/manual/en/session.upload-progress.php
是否有其他方法可以使用PHP和AJAX显示上传进度条但不使用PHP的任何外部扩展?我无权访问php.ini
答案 0 :(得分:36)
PHP Doc非常详细,它说
当上传正在进行时,以及当session.upload_progress.name INI设置设置为POST的同名变量时,上传进度将在$ _SESSION超全局中可用。当PHP检测到此类POST请求时,它将填充$ _SESSION中的数组,其中索引是session.upload_progress.prefix和session.upload_progress.name INI选项的连接值。通常通过读取这些INI设置来检索密钥,即
您需要的所有信息都已在PHP会话命名
中准备就绪您只需要提取此信息并以HTML格式显示。
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css"
rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript">
var intval = null;
var percentage = 0 ;
function startMonitor() {
$.getJSON('b.php',
function (data) {
if (data) {
percentage = Math.round((data.bytes_processed / data.content_length) * 100);
$("#progressbar").progressbar({value: percentage});
$('#progress-txt').html('Uploading ' + percentage + '%');
}
if(!data || percentage == 100){
$('#progress-txt').html('Complete');
stopInterval();
}
});
}
function startInterval() {
if (intval == null) {
intval = window.setInterval(function () {startMonitor()}, 200)
} else {
stopInterval()
}
}
function stopInterval() {
if (intval != null) {
window.clearInterval(intval)
intval = null;
$("#progressbar").hide();
$('#progress-txt').html('Complete');
}
}
startInterval();
</script>
session_start();
header('Content-type: application/json');
echo json_encode($_SESSION["upload_progress_upload"]);
以下是来自PHP Session Upload Progress
的更优化版本$('#fileupload').bind('fileuploadsend', function (e, data) {
// This feature is only useful for browsers which rely on the iframe transport:
if (data.dataType.substr(0, 6) === 'iframe') {
// Set PHP's session.upload_progress.name value:
var progressObj = {
name: 'PHP_SESSION_UPLOAD_PROGRESS',
value: (new Date()).getTime() // pseudo unique ID
};
data.formData.push(progressObj);
// Start the progress polling:
data.context.data('interval', setInterval(function () {
$.get('progress.php', $.param([progressObj]), function (result) {
// Trigger a fileupload progress event,
// using the result as progress data:
e = document.createEvent('Event');
e.initEvent('progress', false, true);
$.extend(e, result);
$('#fileupload').data('fileupload')._onProgress(e, data);
}, 'json');
}, 1000)); // poll every second
}
}).bind('fileuploadalways', function (e, data) {
clearInterval(data.context.data('interval'));
});
$s = $_SESSION['upload_progress_'.intval($_GET['PHP_SESSION_UPLOAD_PROGRESS'])];
$progress = array(
'lengthComputable' => true,
'loaded' => $s['bytes_processed'],
'total' => $s['content_length']
);
echo json_encode($progress);
答案 1 :(得分:4)
这是我的代码工作正常试试:
演示网址
http://codesolution.in/dev/jQuery/file_upload_with_progressbar/
请尝试以下代码: -
这是我的HTML代码
<!doctype html>
<head>
<title>File Upload Progress Demo #1</title>
<style>
body { padding: 30px }
form { display: block; margin: 20px auto; background: #eee; border-radius: 10px; padding: 15px }
.progress { position:relative; width:400px; border: 1px solid #ddd; padding: 1px; border-radius: 3px; }
.bar { background-color: #B4F5B4; width:0%; height:20px; border-radius: 3px; }
.percent { position:absolute; display:inline-block; top:3px; left:48%; }
</style>
</head>
<body>
<h1>File Upload Progress Demo #1</h1>
<code><input type="file" name="myfile"></code>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="uploadedfile"><br>
<input type="submit" value="Upload File to Server">
</form>
<div class="progress">
<div class="bar"></div >
<div class="percent">0%</div >
</div>
<div id="status"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script src="http://malsup.github.com/jquery.form.js"></script>
<script>
(function() {
var bar = $('.bar');
var percent = $('.percent');
var status = $('#status');
$('form').ajaxForm({
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
bar.width("100%");
percent.html("100%");
status.html(xhr.responseText);
}
});
})();
</script>
</body>
</html>
我的upload.php文件代码
<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>
答案 2 :(得分:4)
我建议你FileDrop。
我用它做了一个progess吧,这很容易。
我遇到的唯一缺点是处理大量数据的一些问题,因为它似乎无法清除旧文件 - 可以手动修复。
不是作为JQuery编写的,但无论如何它都非常好,作者可以很快地回答问题。
答案 3 :(得分:3)
虽然为进度条编写代码可能会很有趣,但为什么不选择现有的实现呢? Andrew Valums写了一篇很棒的文章,你可以在这里找到它:
我在所有项目中使用它,它就像一个魅力。
答案 4 :(得分:0)
首先,确保您的计算机上安装了PHP 5.4。你没有标记php-5.4,所以我不知道。通过从命令行调用echo phpversion();
(或php -v
)进行检查。
无论如何,假设您拥有正确的版本,您必须能够在php.ini
文件中设置正确的值。既然你说你不能这样做,那么就不值得我开始解释如何去做。
作为后备解决方案,请使用Flash对象上传器。
答案 5 :(得分:0)
XMLHTTPREQUSET2
var xhr = new XMLHttpRequest();
xhr.open('GET', 'video.avi', true);
xhr.responseType = 'blob';
xhr.onload = function(e) {
if (this.status == 200) {
var blob = this.response;
/*
var img = document.createElement('img');
img.onload = function(e) {
window.URL.revokeObjectURL(img.src); // Clean up after yourself.
};
img.src = window.URL.createObjectURL(blob);
document.body.appendChild(img);
/*...*/
}
};
xhr.addEventListener("progress", updateProgress, false);
xhr.send();
function updateProgress (oEvent) {
if (oEvent.lengthComputable) {
var percentComplete = oEvent.loaded / oEvent.total;
console.log(percentComplete)
} else {
// Unable to compute progress information since the total size is unknown
}
}