Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.1 using session.upload_progress

I want to build a progess bar with the status of my php script. I have read that it could be done with using session.upload_progress.

I'm using laravel Homestead and in the php.ini all requerements are active.

This is my html

{!! Form::open(['route' => 'gebruikers_upload', 'class' => 'form-horizontal import', 'enctype' => 'multipart/form-data', 'target' => 'hidden_iframe']) !!}
                            <input type="hidden" value="myForm" name="{{ini_get("session.upload_progress.name")}}">
                            <input type="file" name="file" id="the-file"/>
                            <button class="btn btn-sm btn-info btn_import" type="submit">Importeer</button>
                            <button class="btn btn-sm btn-danger" type="button">Cancel</button>
                        {!! Form::close() !!}
                        {{--End Form--}}

                        <iframe id="hidden_iframe" name="hidden_iframe" src="about:blank"></iframe>

                        <div class="progress">
                            <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="25"
                                    aria-valuemin="0" aria-valuemax="100" style="width: 45%">
                                <span class="">45% Complete</span>
                            </div>
                        </div>

When submiting te route is:

Route::get('dashboard/gebruikers/upload_status', 'UserController@uploadStatus');

And in the controller UserController in method uploadStatus i have this

public function uploadStatus(Request $request)
{
    session_start();

    echo '<pre>';
        print_r($_SESSION);
    echo '</pre>';
}

But it always shows an empty array. And when i use this code

    $data = $request->session()->all();

    echo '<pre>';
    print_r($data);
    echo '</pre>';

It returns this

Array
(
    [_token] => jFkleI9kIZJiZP3pEARx0hDrHtsynPmuGkse97nT
    [_previous] => Array
        (
            [url] => http://localhost.dev:8000/dashboard/gebruikers/upload_status
        )

    [flash] => Array
        (
            [old] => Array
                (
                )

            [new] => Array
                (
                )

        )

    [login_82e5d2c56bdd0811318f0cf078b78bfc] => 1
)

But there is no info about progress updating.

How could i use this with laravel 5.1

like image 212
Bham Avatar asked Oct 23 '15 14:10

Bham


1 Answers

I encountered this when attempting to solve a similar problem: updating the progress of a long-running process on the client side.

The problem comes down to this: when you make a request to the server, the session values are not written to the session store until the response is returned to the client. Basically, the process looks like this:

        CLIENT         |        SERVER
                       |             .
send request       ----|----> start processing
                       |             .
                       |             .
                       |             .
                       |     finish processing
                       |     write session values
receive response  <----|---- return response

What you're doing is something like this:

        CLIENT         |        SERVER
                       |             .
send request       ----|----> start processing
                       |             .
                       |      (update session)
                       |             .
                       |      (update session)
                       |             .
                       |      (update session)
                       |             .
                       |     finish processing
                       |     write session values
receive response  <----|---- return response

But those (update session) calls on the server are ignored - they're not actually getting written to the session store. So when the client makes a call to your route to request the session value for the progress, it's not getting anything.

The solution is simple: write the progress value somewhere else. A long time ago I solved this by writing the value into a file using file_put_contents. If I were doing it today I'd probably look at a redis server for better efficiency.

One thing to note, however: if you choose to write the value somewhere else, you need to have some way of associating the value with the session. Otherwise, other users are going to overwrite your value. For a simple project, I'd probably use the user's ID value (assuming that they're only going to be processing one thing at a time).

like image 139
Kryten Avatar answered Sep 22 '22 09:09

Kryten