2

I googled and saw that there are some plugins that do this. But would not want to use plugin because my only problem is this. Through Yii is possible in the session variable to obtain data regarding the progress of uploading a file?I'm making a system that uses php and Yii framework. I'm building a module to upload files. Everything is working perfectly except the display of the progress bar.

I researched and found that the version of PHP 5.4 has the capability forward: Session Upload Progress (http://php.net/manual/en/session.upload-progress.php)

I enabled this setting in my php.ini. I am using File Upload plugin to assist in the client side to perform file uploads. (github.com/blueimp/jQuery-File-Upload)

I want to run the following code:

Client side:

$('#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(yii.baseUrl + '/atl/default/progressUpload', $.param([progressObj]), function(result) {
                        // Trigger a fileupload progress event,
                        // using the result as progress data:
                        e = $.Event('progress', {
                            bubbles : false,
                            cancelable : true
                        });
                        $.extend(e, result);
                        ($('#fileupload').data('blueimp-fileupload') || $('#fileupload').data('fileupload'))._onProgress(e, data);
                    }, 'json');
                }, 1000)); // poll every second
            }
        }).bind('fileuploadalways', function(e, data) {
            clearInterval(data.context.data('interval'));
        });

        $('#fileupload').fileupload({
            dataType : 'json',
            axFileSize : 500000000,
            forceIframeTransport : true,
            add : function(e, data) {
                data.context = $('<button/>').text('Upload').appendTo(document.body).click(function() {
                    data.context = $('<p/>').text('Uploading...').replaceAll($(this));
                    data.submit();
                });
            },
            done : function(e, data) {
                alert("done!");
            },
            progressall : function(e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                console.log(e, data, progress);
                $('#progress .bar').css('width', progress + '%');
            }
        });

Server Side:

public function actionProgressUpload($PHP_SESSION_UPLOAD_PROGRESS) {

        $s = Yii::app()->session['upload_progress_'.intval($PHP_SESSION_UPLOAD_PROGRESS)];
        $progress = array(
                'lengthComputable' => true,
                'loaded' => $s['bytes_processed'],
                'total' => $s['content_length']
        );
        echo json_encode($progress);
    }

My problem:

Yii in the $ _SESSION variable always returns null, the variable Yii::app()->session does not return me the values ​​for upload progress.

I googled and saw that there are some plugins that do this. But would not want to use plugin because my only problem is this. Through Yii is possible in the session variable to obtain data regarding the progress of uploading a file?

Thanks.

9
  • You sure you initialized first your custom session Yii::app()->session['upload_progress_'.intval($PHP_SESSION_UPLOAD_PROGRESS)] before you obtained its value? Commented Mar 12, 2014 at 15:35
  • The session is initialized automatically by Yii. Commented Mar 12, 2014 at 17:47
  • What I meant was is your 'upload_progress_' . intval(...) session key was initialized? Commented Mar 13, 2014 at 1:42
  • This variable is set by PHP while uploading files. Take a look at: php.net/manual/en/session.upload-progress.php Commented Mar 13, 2014 at 14:37
  • 1
    Also, "Note, this feature doesn't work, when your webserver is running PHP via FastCGI." Are you? Commented Mar 18, 2014 at 9:44

1 Answer 1

1

protected/config/main.php

Check whether the session is set to false in main config. if it so then i wont work

'session' => array ( 'autoStart' => false, ),

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.