0

Is there a way I could get objects of type splfileinfo with laravel storage disks? I need a common way of sending an instance that wraps a file (stored on local or s3 service) to a AnalyzeImageService.

I thought that an instant of splfileinfo would be great as it has all the methods that my service needs (getSize,etc).

Question: how can I get a \splfileinfo object that wraps a file given a disk from laravel.

lesssugar
  • 15,486
  • 18
  • 65
  • 115
user237329
  • 809
  • 1
  • 10
  • 27

1 Answers1

-2

Hello user2373229,

In order to set up the custom filesystem you will need to create a service provider. In the provider's boot method, you may use the Storage facade's extend method to define the custom driver.

<?php

namespace App\Providers;

use Storage;
use \SplFileInfo as SplFileInfo;
use Illuminate\Support\ServiceProvider;


class SplFileInfoServiceProvider extends ServiceProvider
{
/**
 * Perform post-registration booting of services.
 *
 * @return void
 */
public function boot()
{
    Storage::extend('splfileinfo', function () {

       return new SplFileInfo;
    });
}

/**
 * Register bindings in the container.
 *
 * @return void
 */
public function register()
{
    //
}
}

Once you have created the service provider to register the extension, you may use the splfileinfo driver in your config/filesystems.php configuration file.

You may refer to the docs: https://laravel.com/docs/5.3/filesystem#custom-filesystems

PHPGrandMaster
  • 358
  • 2
  • 10
  • How do you get a collection of `SplFileInfo` instances for all files in a directory similar to `\Storage::disk($disk)->allFiles($path)` (except that the returned collection would be of type `SplFileInfo`, ofc)? – Chad Feb 10 '19 at 01:06