1

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: Illuminate\Contracts\Filesystem\FileNotFoundException

But file already exists!

I want read file contents.


Laravel Version: 6.2.0 - PHP Version: 7.2.19

SayJeyHi
  • 1,559
  • 4
  • 19
  • 39
  • Can you `dd(storage_path('logs/laravel-2019-10-14.log'))` to see the actual path? Otherwise might be permission issue? – senty Oct 17 '19 at 15:05
  • I do and see this result: "/var/www/html/myLaravelLab/storage/logs/laravel-2019-10-14.log" I give permission 777 to all folders and files ... But still see that error!!! – Morteza Nasiri Oct 17 '19 at 15:11

4 Answers4

3

In config/filesystems.php add new disk logs:

'disks' => [
        'logs' => [
            'driver' => 'local',
            'root' => storage_path('logs'),
        ],
// orher disks...

Now you can get log files:

Storage::disk('logs')->get('laravel-2019-10-14.log');
0

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:

Storage::get('logs/laravel-2019-10-14.log')

See where the disk is searching for the file, then adjust the path until you are searching the correct directory.

rborum
  • 124
  • 1
  • 11
0

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:

$contents = Storage::get('../logs/laravel-2019-10-14.log');

or

$contents = file_get_contents(storage_path('logs/laravel-2019-10-14.log'));

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 the Storage::get() method you will need to adjust the location in your config. In config/filesystems.php

'disks' => [

    'local' => [
        'driver' => 'local',
        // 'root' => storage_path('app'),
        'root' => storage_path(),
    ],
]

then you can use

$contents = Storage::get('logs/laravel-2019-10-14.log');
Jon Allen
  • 335
  • 1
  • 9
0

Perhaps the Storage class is overkill for your implementation. You could just use the PHP file_get_contents() function instead:

$contents=file_get_contents( storage_path( 'logs/laravel-2019-10-14.log' ) );
Andrew Vickers
  • 2,504
  • 2
  • 10
  • 16