Wallogit.com
2017 © Pedro Peláez
This package allows you to store files on any place, even local filesystem, or cloud CDN, FTP servers etc., (*1)
To accomplish this, a common way to describe a file location is needed., (*2)
Let's assume that you have a file stored on /home/user/image.jpg, (*3)
We can tell about this file that it:, (*4)
file storage schema/home/user/image.jpg locationSo, finally, we can describe it as file:///home/user/image.jpg, (*5)
In conclusion, any file is always stored in a location that matches the following pattern of URI:, (*6)
<schema>://<location>[?<query>][#<fragment>]
For example if we store a file in openstack onbject storage we can have the following uri for it:, (*7)
openstack://4281c348eaf83e70ddce0e07221c3d28
Edit the app/config/app.php and add the following lines, (*8)
'aliases' =>
...
'Storage' => 'Softservlet\FileManager\StorageFacade'
'providers' =>
...
'Softservlet\FileManager\FileManagerServiceProvider'
Firstly we will instantiate a new FileInterface implementation object, which accpets as parameter the URI of the FileInterface., (*9)
By default, if the URI doesn not conaint a schema then the file:// schema will be interpreted, (*10)
use Softservlet\FileManager\File\GenericFile;
//See Softservlet\FileManager\File\FileInterface
$file = new GenericFile('/home/user/image.jpg');
We will call then the StorageFactory class to store the file (for this Storage facade is used here)., (*11)
The store method accepts as parameter an FileInterface and optionally an StorageInterface instance., (*12)
If the StorageInterface is not given, the file will be stored using the FilesystemStorage Driver., (*13)
$uri = Storage::store($file);
If you have an openstack storage implementation you will can call, (*14)
$uri = Storage::store($file, new OpenstackStorage)
We stored a file, now we want to retrieve it from the storage mechanism., (*15)
For this, we will use the fileDescriptor() method declared on StorageInterface, and wich is implemented by each storage driver., (*16)
It accepts as parameter an FileInterface and return an FileDescriptorInterface., (*17)
//we stored the file and got the $uri variable $file = new GenericFile($uri); $descriptor = Storage::fileDescriptor($file); //See Softservlet\FileManager\File\FileDescriptorInterface $contents = $descriptor->contents(); $mime = $descriptor->mime();
To get an http url for accessing a file, you may use an implementation of DeliveryInterface., (*18)
Until now, there is an implementation for file stored with FilesystemStorage driver., (*19)
use Softservlet\FileManager\Deliver\LocalHttpDelivery; $file = new GenericFile($uri); $delivery = new LocalHttpDelivery(); $url = $delivery->httpUrl($file);