延迟填充session.upload_progress数据 [英] Delay in populating session.upload_progress data
问题描述
我正在尝试检查上传文件的进度.我正在使用带有Session类的Kohana框架,但是对于上传进度,我正在使用本机PHP会话.我在Kohana的bootstrap.php中调用session_start()
,这意味着在每个页面请求中都会调用session_start()
.
I'm trying to check the progress of files uploaded. I'm using the Kohana framework which has a Session class, but for the upload progress I'm using native PHP sessions. I'm calling session_start()
in Kohana's bootstrap.php, which means session_start()
will be called on every page request.
提交上传表单后,我等待1秒钟,然后开始调用PHP文件以使用jQuery $.ajax()
检查上传进度.
After the upload form is submitted, I wait 1 second and then begin calling a PHP file to check the upload progress using jQuery $.ajax()
.
问题是在第一次调用PHP时未设置$_SESSION[$key]
($ key包含用于上传数据的密钥).我已经尝试了很多调试,并且session_id()
返回正确的会话ID,因此该会话肯定是正确的会话并且处于活动状态.我还要等待1秒钟,然后再检查上传进度,所以这不是时间问题.即使未设置$_SESSION[$key]
,我也可以通过继续操作来解决此问题,但检查上传是否完成的方法是未设置$_SESSION[$key]
时.
The problem is that $_SESSION[$key]
($key contains the key for the upload data) isn't set on the first call to the PHP. I've tried debugging this quite a bit, and session_id()
returns the correct session ID, so the session is definitely the right one and is active. I'm also waiting 1 second before checking the upload progress, so it's not a timing issue. I could fix this by continuing even if $_SESSION[$key]
is not set, but the way to check if the upload is complete is when $_SESSION[$key]
is unset.
HTML表单是使用jQuery动态创建的,因为这是多文件上传.这是生成表单的HTML:
The HTML form is created on-the-fly with jQuery because this is a multi-file upload. Here's the HTML for a generated form:
<form action="ajax/upload" id="form-HZbAcYFuj3" name="form-HZbAcYFuj3" method="post" enctype="multipart/form-data" target="frame-HZbAcYFuj3">
<iframe id="frame-HZbAcYFuj3" name="frame-HZbAcYFuj3"></iframe>
<input type="hidden" name="PHP_SESSION_UPLOAD_PROGRESS" value="HZbAcYFuj3">
<input type="file" id="file-HZbAcYFuj3" name="photo" accept="image/jpeg,image/pjpeg,image/png,image/gif">
<button type="button">+ Select Photo</button>
</form>
这是JavaScript调用以检查进度的PHP:
Here's the PHP that the JavaScript calls to check the progress:
public function action_uploadprogress()
{
$id = isset($_POST['id']) ? $_POST['id'] : false;
if (!$id)
throw new Kohana_HTTP_Exception_404();
$progress = 0;
$upload_progress = false;
$key = ini_get("session.upload_progress.prefix") . $id;
if (isset($_SESSION[$key]))
$upload_progress = $_SESSION[$key];
else
exit('100');
$processed = $upload_progress['bytes_processed'];
$size = $upload_progress['content_length'];
if ($processed <= 0 || $size <= 0)
throw new Kohana_HTTP_Exception_404();
else
$progress = round(($processed / $size) * 100, 2);
echo $progress;
}
这是jQuery ajax()
请求:
Here's the jQuery ajax()
request:
this.send_request = function()
{
$.ajax(
{
url: 'ajax/uploadprogress',
type: 'post',
dataType: 'html',
data: { id: _this.id },
success:
function(data, textStatus, jqXHR)
{
if (textStatus == "success")
{
if (data < 100)
setTimeout(_this.send_request, 1000);
}
}
}
);
};
推荐答案
您正在向PHP脚本发送名为"id"的POST.
You are sending a POST called 'id' to the PHP script.
但是,文档指出,仅当您发送与php.ini中配置的session.upload_progress.name相同名称的POST时,上传进度才可用.
However, the documentation says that the upload progress will be available only when you send a POST with same name as session.upload_progress.name configured in php.ini.
因此,换句话说,如果将session.upload_progress.name设置为默认值(PHP_SESSION_UPLOAD_PROGRESS),则必须在send_request函数中更改以下行:
So, in other words, if your session.upload_progress.name is set to default value (PHP_SESSION_UPLOAD_PROGRESS), you have to change the following line, in send_request function:
更改:
data: { id: _this.id }
收件人:
data: { PHP_SESSION_UPLOAD_PROGRESS: _this.id }
您还必须在PHP脚本中将$_POST['id']
更改为$_POST['PHP_SESSION_UPLOAD_PROGRESS']
或$_POST[ini_get("session.upload_progress.name")]
(如果不是默认输入,则还要更改输入名称).
You also have to change the $_POST['id']
to $_POST['PHP_SESSION_UPLOAD_PROGRESS']
or $_POST[ini_get("session.upload_progress.name")]
in the PHP script (and the name of input too in case it's not default).
在上载过程中,以及在POST张贴与session.upload_progress.name INI设置相同名称的变量时,上载进度在$ _SESSION超全局变量中可用.当PHP检测到此类POST请求时,它将在$ _SESSION中填充一个数组,其中索引是session.upload_progress.prefix和session.upload_progress.name INI选项的串联值.通常,通过读取这些INI设置即可检索密钥,即
The upload progress will be available in the $_SESSION superglobal when an upload is in progress, and when POSTing a variable of the same name as the session.upload_progress.name INI setting is set to. When PHP detects such POST requests, it will populate an array in the $_SESSION, where the index is a concatenated value of the session.upload_progress.prefix and session.upload_progress.name INI options. The key is typically retrieved by reading these INI settings, i.e.
来源: http://php.net/manual/pt_BR/session. upload-progress.php
这篇关于延迟填充session.upload_progress数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!