
Failed writes with Laravel Filesystem
24 Sep 2022 #development #laravel #php
When you use the Laravel Filesystem classes, by default, it doesn't throw any exceptions.
For example, when you write a file, you have to check the return value to see if it was written or not:
if (! Storage::put('file.jpg', $contents)) { // The file could not be written to disk...}
I personally find this a little tricky as it's one of those things which are easy to forget and overlook. I much prefer that an exception is thrown instead.
According to the Laravel documentation, you can do this:
If you wish, you may define the
throw
option within your filesystem disk's configuration array. When this option is defined astrue
, "write" methods such as put will throw an instance ofLeague\Flysystem\UnableToWriteFile
when write operations fail.
'public' => [ 'driver' => 'local', // ... 'throw' => true,],