5

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

0

2 Answers 2

5
+25

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).

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

1 Comment

That's correct, if you were to use illuminate/session package outside Laravel you will need to call the save method on the SessionManager instance to save the updates to the $_SESSION variable.
1

Laravel Documentation has all the methods you need to mess with the session.

$request->session()->put('key', 'value');

You can simply put anything you wish to the session like this.

As a helper shorthand Laravel offers you session() helper function. Which accept an array of "key value pairs" to set values or only a key to retrieve any value from the session.

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.