Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

k0tkin's avatar

L5: \File::get($filePath) kill my session

Hi,

I'm trying to show images for each of my clients from storage directory instead of public directory to handling unauthorized access to images but there is a big problem after requesting more than about 20 response()->make(\File::get($filePath), 200, ['Content-Type' => 'image/png']); laravel kill my session and i need to log in to my application again and another big problem is my cpu usage is 100% each time i request this 20 images from my storage directory, any idea what's going on?

I'm using WAMP in windows 8.1 for my local server

0 likes
9 replies
bashy's avatar

Sounds like what you're trying to do is memory/CPU intensive... How do you get them all, in a loop?

k0tkin's avatar

Each user in my application has a lot of images, i want to show my client at least 20 images in each page and here is my code and how i retrieve this images

HTML code:

<div class="row" >
    <div class="col-xs-6 col-md-4" ng-repeat="image in images">
        <a href="#" class="thumbnail">
            <img ng-src="@{{ getImage(image.display_name) }}">
        </a>
    </div>
</div>

AngularJS code:

app.controller('GalleryController', ['$scope', '$http', 'number', function ($scope, $http, number)
{
    $http.get('images/' + number).success(function (data)
    {
        $scope.images = data;
    });

    $scope.getImage = function(display_name)
    {
        return 'images/' + number + '/' + display_name;
    }
}
]);

Laravel Code

public function image($device_number, $display_name)
{
    $device = \Auth::user()->device($device_number);

    $fileName = \Cache::remember('device_number_' . $device_number . '_' . $display_name, 10, function () use ($device, $display_name)
    {
        return $device->images()->where('display_name', $display_name)->first(array('file_name'))->file_name;
    });

    $filePath = storage_path() . "/app/devices/" . $device->directory_name . "/" . $fileName;

    return response()->make(\File::get($filePath), 200, ['Content-Type' => 'image/png']);
}
khoanguyenme's avatar

It's a heavy job. Perhaps you can try response()->download($path, $optional_filename)

k0tkin's avatar

With response()->download() problem is still the same, laravel kill my session after loading 20 images and i need to login to my application again Why loading 20 images is heavy? Let's imagine it's a heavy job why laravel kill my session because of that? (can i turn it off?)

Or another question: How to show images SECURELY (i don't want to use public directory to store my client private images)

k0tkin's avatar

my problem solved by changing

<img ng-src="@{{ getImage(image.display_name) }}"> 

to

<img src="@{{ getImage(image.display_name) }}">

It seems ng-src send all 20 image request at the same time but src request one by one

but i don't like this solution, i like to know why laravel have this behavior

jabba's avatar

I also had similar problems when using model named "File".

If you take a look at /config/app.php, there you can see following alias:

'File'      => 'Illuminate\Support\Facades\File',

If you try to dd($your_response), then your computer will hang for some time, and you will see a dump with many recursion functions, which are the real cause of this behaviour. Just rename File model into something else, and everything will work normal.

k0tkin's avatar

By changing File model name problem is still available, and by changing ng-src to src problem solved for just couple of minutes and now problem is still there :(

k0tkin's avatar

I'm still trying but no success :( it seems i can't secure clients photos and i need to put them in public folder it's really bad :((

poldixd's avatar

I have the same problem. If i load 15 images or more my session killed by laravel and the user must re-login. Do you have some new news about this issue?

Please or to participate in this conversation.