会话上传进度 [英] Session Upload Progress

查看:67
本文介绍了会话上传进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取会话上传进度功能( http://php.net/manual/en/session.upload-progress.php )在Kohana中工作.我已经成功使用以下代码在没有Kohana的情况下使其在本地运行:

I'm trying to get the Session Upload Progress feature ( http://php.net/manual/en/session.upload-progress.php ) to work in Kohana. I have managed to get it working locally without Kohana using the following code:

<?php
    session_start();
    if (isset($_GET['progress']))
    {
        // does key exist
        $key = ini_get("session.upload_progress.prefix") . 'demo';
        if ( !isset( $_SESSION[$key] ) ) exit( "uploading..." );

        // workout percentage
        $upload_progress = $_SESSION[$key];
        $progress = round( ($upload_progress['bytes_processed'] / $upload_progress['content_length']) * 100, 2 );

        exit( "Upload progress: $progress%" );
    }
?>
<!doctype html>
<head>
</head>
<body>
    <section>
        <h1>Upload Form</h1>
        <form action="" method="POST" enctype="multipart/form-data" target="upload-frame">
            <input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="<?php //echo $uid; ?>demo">
            <p>
                <label>File:</label>
                <input type="file" name="file" required="required">
            </p>
            <p><input type="submit" name="submit" value="Upload"></p>
        </form>

        <iframe id="upload-frame" name="upload-frame" width="1280" height="600"></iframe>

        <div id="file_upload_progress"></div>
    </section>

    <script src="jquery-1.7.1.min.js"></script>
    <script>
        $(document).ready(function() {

            var uploading = false;
            $('form').submit(function() {

                uploading = true;
                $('#upload-frame').one('load', function(){
                    uploading = false;
                });

                function update_file_upload_progress() {
                    $.get("?progress", function(data) {
                        $("#file_upload_progress").html(data);
                        if (uploading) {
                            setTimeout( update_file_upload_progress, 500 );
                        }
                    })
                    .error(function(jqXHR, error) { 
                        alert(error); 
                    });
                }

                // first call
                update_file_upload_progress();
            });
      });
    </script>
</body>
</html>

但是,当我在Kohana中使用此代码(当然将PHP分离为控制器)时,不会创建$_SESSION变量来跟踪上传进度.

However when I use this code in Kohana (separating the PHP into a controller of course) the $_SESSION variable does not get created for tracking the progress of the upload.

我相信这与Kohana中的会议如何工作有关.我不能在脚本的开头加上session_start(),因为它与已经在运行的Kohana会话冲突.如果我转储$_SESSIONSession::instance()内容,则应该通过PHP Upload Progress功能添加的变量不存在.

I believe this is something to do with how sessions in Kohana work. I cannot have session_start() at the start of the script as it conflicts with the Kohana session that's already running. If I dump out the $_SESSION or Session::instance() contents the variable that should be added by the PHP Upload Progress functionality isn't there.

那么我如何使会话变量与Kohana一起使用?

So how do I get the session variable to work with Kohana?

更新

此后,我创建了Kohana的全新安装,以帮助缩小此问题的范围.我发现,通过不实例化Kohana中的Session类,我可以使用上面的代码,并且可以正常工作.

I have since created a clean install of Kohana to help narrow down on this issue. I have found that by not instantiating the Session class in Kohana that I can use the code above and it works fine.

但是,当实例化Session类时,它对于我的Web应用程序来说是必需的,它将停止工作,并且不再创建包含上载进度的$_SESSION变量.这使我相信问题出在Kohana如何管理会话信息之内.我尝试通过配置设置关闭加密功能,但这没什么用.

However when the Session class is instantiated which it needs to be for my web application it stops working and the $_SESSION variable containing upload progress is no longer created. This leads me to believe that the issue lies somewhere within how Kohana manages session information. I tried turning off the encryption with config settings but that didn't make a difference.

我正在使用本机会话.

推荐答案

会话cookie名称必须与php config(session.name)中设置的名称完全相同,例如:

Session cookie name has to be exactly the same as the one set in php config (session.name), e.g.:

return array(
    'native' => array(
        'encrypted' => FALSE,
        'name'      => ini_get('session.name'),
    ),
);

如果您不想使用PHP的默认会话cookie名称,则不会通过在运行时设置值来解决这个问题,即,这将不起作用:

If you don't want to use PHP's default session cookie name you will not get around this by setting the value during runtime, i.e. this won't work:

ini_set('session.name', 'my_kohana_session_name');

通过在.htaccess文件中设置值来解决此问题:

You will get around this by setting the value in your .htaccess file:

php_flag session.name "my_kohana_session_name"

这样,您可以保持php.ini不变,但是可以为Kohana应用程序保留自定义cookie名称.

This way you can keep the php.ini untouched but you can keep your custom cookie name for your Kohana application.

我的测试证明,使用本机会话驱动程序时,会话加密不会影响上传进度信息.可以肯定的是,由于在使用本机驱动程序时不使用加密,因此会使该设置无效. ;)

My tests proved that session encryption does not affect the upload progress information when using native session driver. That surely because the encryption is not used when using native driver regaldess the setting. ;)

PS.我还建议一件事-确保不缓存ajax请求.有很多情况下,将其缓存并没有显示新的响应.

PS. One thing I would also suggest - make sure that the ajax request is not cached. Had quite a few cases when it was cached and not showing fresh responses.

这篇关于会话上传进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆