我正在尝试让会话上传进度功能(http://php.net/manual/en/session.upload-progress.php)在 Kohana 中工作。我已经设法让它在没有 Kohana 的情况下使用以下代码在本地工作:
<?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不会创建变量来跟踪上传进度。
我相信这与 Kohana 中的会话如何工作有关。我不能session_start()在脚本的开头使用,因为它与已经运行的 Kohana 会话冲突。如果我转储$_SESSION或Session::instance()内容,则应由 PHP 上传进度功能添加的变量不存在。
那么如何让会话变量与 Kohana 一起使用呢?
更新
我已经创建了一个全新的 Kohana 安装来帮助缩小这个问题的范围。我发现通过不实例化SessionKohana 中的类,我可以使用上面的代码并且它工作正常。
但是,当Session实例化需要用于我的 Web 应用程序的类时,它会停止工作,并且$_SESSION不再创建包含上传进度的变量。这让我相信问题在于 Kohana 如何管理会话信息。我尝试使用配置设置关闭加密,但这并没有什么不同。
我正在使用本机会话。