使用PHP获取上传进度 [英] Getting Upload progress with PHP
问题描述
我无法使用PHP获取文件上传的进度,我的网站托管的服务器正在使用php v5.6.
我也在cpanel管理器中启用了"uploadprogress"设置.
我尝试按照本教程进行操作: 使用PHP和JavaScript跟踪上传进度 >
但是我将其与其他一些代码合并在一起,这些代码使用ajax请求来处理文件的上载.
我的表单代码:
<form id="myForm">
<input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
<input type="file" id="file" />
<a href="javascript:uploadFile()">Upload</a>
</form>
Upload:
<div id="process">0%</div>
JavaScript代码: var timer;
function uploadFile() {
var uploadFile = $("#file")[0].files[0];
timer = setInterval(getStatus,1000);
$.ajax(
{
url : "upload.php",
type: 'POST',
data : new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress',
function(e) {
if (e.lengthComputable) {
if (e.loaded < e.total) {
$('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%");
} else {
$('#process').html("Processing upload, please wait.");
}
}
} , false);
}
return myXhr;
},
success: function (response) {
$('#process').html(response);
clearTimeout(timer);
}
}
);
}
function getStatus() {
$.get("progress.php",
function(response) {
$("#process2").html(response);
}
);
}
upload.php
session_start();
$uploadedFile = $_FILES["file"];
move_uploaded_file($uploadedFile['tmp_name'],$uploadedFile["name"]);
echo $uploadedFile["name"]." was uploaded";
progress.php
session_start();
$key = ini_get("session.upload_progress.prefix") . "myForm";
if (!empty($_SESSION[$key])) {
$current = $_SESSION[$key]["bytes_processed"];
$total = $_SESSION[$key]["content_length"];
echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
echo 100;
}
不幸的是,在我的JS行中: $('#process').html((((e.loaded/e.total)* 100).toFixed(1)+%");
仅获取获取请求所花费的时间,而不实际获取文件.
希望我能提供足够的信息以获取帮助.
您必须确保您的服务器正在使用mod_php运行PHP.
我正在nginx上运行Drupal并警告我
您的服务器无法显示文件上传进度.文件上传进度要求运行PHP且运行mod_php的Apache服务器."
I am having trouble getting the progress of a file upload using PHP, the server that my site is hosted on is using php v5.6.
I also have the 'uploadprogress' setting enabled in my cpanel manager.
I tried to follow this tutorial: Tracking Upload Progress with PHP and JavaScript
But I merged it with some other code that handles uploading of files using an ajax request.
My form code:
<form id="myForm">
<input type="hidden" value="myForm" name="<?php echo ini_get("session.upload_progress.name"); ?>">
<input type="file" id="file" />
<a href="javascript:uploadFile()">Upload</a>
</form>
Upload:
<div id="process">0%</div>
Javascript code: var timer;
function uploadFile() {
var uploadFile = $("#file")[0].files[0];
timer = setInterval(getStatus,1000);
$.ajax(
{
url : "upload.php",
type: 'POST',
data : new FormData($('form')[0]),
cache: false,
contentType: false,
processData: false,
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress',
function(e) {
if (e.lengthComputable) {
if (e.loaded < e.total) {
$('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%");
} else {
$('#process').html("Processing upload, please wait.");
}
}
} , false);
}
return myXhr;
},
success: function (response) {
$('#process').html(response);
clearTimeout(timer);
}
}
);
}
function getStatus() {
$.get("progress.php",
function(response) {
$("#process2").html(response);
}
);
}
upload.php
session_start();
$uploadedFile = $_FILES["file"];
move_uploaded_file($uploadedFile['tmp_name'],$uploadedFile["name"]);
echo $uploadedFile["name"]." was uploaded";
progress.php
session_start();
$key = ini_get("session.upload_progress.prefix") . "myForm";
if (!empty($_SESSION[$key])) {
$current = $_SESSION[$key]["bytes_processed"];
$total = $_SESSION[$key]["content_length"];
echo $current < $total ? ceil($current / $total * 100) : 100;
}
else {
echo 100;
}
Unfortunately in my JS the line: $('#process').html(((e.loaded / e.total) * 100).toFixed(1) + "%");
only gets the time it takes to get the request, not actually upload the file.
I hope I have provided enough information to get some help.
You have to be sure your server is running PHP with mod_php.
I'm running Drupal on nginx and warns me that
"Your server is not capable of displaying file upload progress. File upload progress requires an Apache server running PHP with mod_php."
这篇关于使用PHP获取上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!