'laravel file_put_contents(): Exclusive locks are not supported for this stream
I tried to upload my site to laravel to host and be configured but I couldn't load the project. The error is as follows:
file_put_contents(): Exclusive locks are not supported for this stream
How do I fix this?
Solution 1:[1]
Try to remove the following line of code in your path:
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, $lock ? LOCK_EX : 0);
}
and replace it with this one below:-
public function put($path, $contents, $lock = false)
{
return file_put_contents($path, $contents, 0);
}
Solution 2:[2]
I had a similar issue recently. Running the artisan command: php artisan config:cache fixed it for me
Solution 3:[3]
I had the same error, while using NFS to share my code from my developer machine to my Centos server. The problem seems to be that the server tried to access my NFS-shared files using NLM locking by default, so I had to disable locking in my settings. This is done by adding nolock in the NFS options in /etc/fstab
This fixed the issue for me
Solution 4:[4]
The problem occurs when running on a system that doesn't support exclusive file locks.
An exclusive or write lock gives a process exclusive access for writing to the specified part of the file. While a write lock is in place, no other process can lock that part of the file.
There are currently 2 places in Laravel where such locks are enforced for data security reasons: when writing cache entries and sessions. Note the third argument to Filesystem::put() is true in both these calls, which causes it to try and gain an exclusive lock before writing.
The way to resolve this issue is to get the filesystem adapter to not attempt that lock. The approach suggested in one answer is to edit Laravel's library files. This is, as the kids say, the smooth-brain approach. Your changes will be lost on upgrade so must be continually managed.
Instead, write your own custom class, override that method, and configure your application to use it.
Step 1: create app/Filesystem/Filesystem.php with these contents:
<?php
namespace App\Filesystem;
use Illuminate\Filesystem\Filesystem as FilesystemBase;
class Filesystem extends FilesystemBase
{
public function put($path, $contents, $lock = false)
{
// don't pass the value of $lock to the parent method
return parent::put($path, $contents);
}
}
Step 2: edit app/Providers/AppServiceProvider.php to register the new class. Don't remove existing registrations, if any, just add this one:
<?php
namespace App\Providers;
use App\Filesystem\Filesystem;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton('files', fn () => new Filesystem);
}
}
And that's all. Confirm your provider is working by firing up php artisan tinker and entering app('files'). You should get an instance of your new class.
Solution 5:[5]
You have to close the server by closing your batch window you used in starting the server initially.
Refresh your browser and make sure the the server has been closed.
Start a new development server by typing
$ php artisan serveGo to your browser and enter the server address generated by the artisan. It's usually http://127.0.0.1:8000
Solution 6:[6]
Best Solution for Laravel Go to /laravel/framework/src/Illuminate/Filesystem/Filesystem.php delete content function " public function put($path, $contents, $lock = false)" line 120. Finish
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Gideon Macha |
| Solution 2 | Dunsin Olubobokun |
| Solution 3 | Stefanos Kargas |
| Solution 4 | miken32 |
| Solution 5 | Alex Myers |
| Solution 6 | cheikh78 ndongo |
