I want read file content in laravel 6, the file already exists but laravel return not found exception.
This is my logs files directory:
/var/www/html/myLaravelLab/storage/logs/laravel-2019-10-14.log
$contents = Storage::get(storage_path('logs/laravel-2019-10-14.log'));
Laravel return this error: IlluminateContractsFilesystemFileNotFoundException
But file already exists!
I want read file contents.
Laravel Version: 6.2.0
– PHP Version: 7.2.19
4
Answers
storage_path('logs/laravel-2019-10-14.log')
will return the full path to the resource, such as:http://www.app-name.com/storage/logs/laravel-2019-10-14.log
.Storage::get() accepts a path relative to the storage disk’s root.
You likely need to not include
storage_path
in your function, and try something like:See where the disk is searching for the file, then adjust the path until you are searching the correct directory.
storage_path will essentially prefix the path you give it with the path to the storage folder, so
/var/www/website.com/storage/logs/laravel-2019-10-14.log
.If you use Storage::path() this works similarly, but from the configured storage disk (so this can be cloud based if you like). The main difference is this looks in
storage/app
instead, so you will need to move your logs in there, or possibly you might be able to say:or
Edit:
I’ve just checked and you can’t use the
Storage::get('../')
as it goes out of the available scope. If you would like to use theStorage::get()
method you will need to adjust the location in your config.In
config/filesystems.php
then you can use
In
config/filesystems.php
add new disklogs
:Now you can get log files:
Perhaps the Storage class is overkill for your implementation. You could just use the PHP file_get_contents() function instead: