18

Issue with session storage

Session storage not working with my Laravel 5. Used it for a month without problems. But since latest upload to web server I get the following response on every page.

ErrorException in Filesystem.php line 74:
file_put_contents(): Exclusive locks are not supported for this stream

Many posts on the internet are indicating to remove the reference to

$lock on line 74 in Filesystem.php

Seems like a quick fix which will lead to other issues down the road...

How do I take care of the root cause?

References

https://stackoverflow.com/questions/29023948/laravel-5-file-put-contents-exclusive-locks-are-not-supported-for-this-stre https://laracasts.com/discuss/channels/general-discussion/l5-new-install-error

Community
  • 1
  • 1
Peder Wessel
  • 646
  • 1
  • 9
  • 23
  • Additional links (did not have enough reputation) https://github.com/octobercms/october/issues/969 https://github.com/laravel/framework/issues/8007#issue-61873798 – Peder Wessel Mar 20 '15 at 15:14
  • Wait, you're putting your session data on a cloud filesystem? Bad idea. – ceejayoz Mar 20 '15 at 16:50
  • I had this problem today with 4.2 and your quickfix has worked. Likewise, it was working before, but uploading today has caused this problem. I am pretty sure this is because of a recent (buggy) Laravel update as I did a `composer update` within the last week. – tomturton Mar 21 '15 at 20:54

7 Answers7

72

after update do

chmod -R gu+w storage

chmod -R guo+w storage

php artisan cache:clear
maleeb
  • 890
  • 7
  • 15
  • 1
    @maleeb you only need to run one 'chmod' command: `chmod -R a+w storage`. You're overwriting group and user 'write' permissions in the second command. its superfluous. – Sebastian Scholle Jun 22 '20 at 09:13
20

If you use Laravel 5.2.0 you've probably ignored the storage folder in the .gitignore file. The thing is that when you run composer install on your server not all the files from the storage folder are created, you should check those !

In my case my storage folder contained only the logs folder without app and framework folders. You need to create those manually so you should have this structure at the end:

storage
  - app
  - framework
    - cache 
    - sessions
    - views
  - logs

Don't forget to make the storage folder writable !

paulalexandru
  • 9,218
  • 7
  • 66
  • 94
6

Had to do with the hosting. My cloud service did not support exclusive locks. I exchanged

return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);

to

return file_put_contents($path, $contents, $lock ? LOCK_SH : 0);

on line 74 of Filesystem.php

Peder Wessel
  • 646
  • 1
  • 9
  • 23
  • 1
    Great solution. Thanks! I exchanged return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); with return file_put_contents($path, $contents, $lock ? LOCK_SH : 0); on line 74 of Filesystem.php works like a charm. – Stelefanten Mar 31 '15 at 14:43
  • Is it safe to do so? – Sapnesh Naik Mar 02 '20 at 17:09
5

thanks @peder-wessel. Your answer is the cause explanation but not a permanent solution and will work until we update the vendors again as Filesystem.php file is inside Laravel vendor directory. One day, when we composer update it will cause problem again. :(

However I was able to find out that this issue is caused because Session is saved to file. I changed this from

'driver' => 'file',

to

'driver' => 'cookie',

in /app/config/session.php

Maybe laravel creators should add this to configuration too, as not all (shared) hostings provide exclusive lock rights

99problems
  • 534
  • 10
  • 22
3

I had the same problem and renaming or removing the

/bootstrap/cache/config.php

file(file name) is working for me.

This file containes/caches the local server configuration which conflicts with the remote server.

Hope this will help. All the best.

1

php artisan config:cache
could help and is probably a better way than removing bootstrap/cache/config.php, it updated it.
If your server works in docker container, be careful, if you have a volume to share with host and run command on host, it put host paths in config.php, paths that doesn't exist in container…
so connect to container and run it inside

bcag2
  • 1,988
  • 1
  • 17
  • 31
0

I had problem with publishing my assets from my package with this error file_put_contents(): Exclusive locks are not supported for this stream

this package helps NoLock

Hassan Gilak
  • 691
  • 9
  • 14