skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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');
    
    Login or Signup to reply.
  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');
    
    Login or Signup to reply.
  4. 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' ) );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search