If you need to handle file storing and reading on various file systems, checkout Flysystem from The PHP League. This extension is implementing Flysystem into your application based on Nette Framework
The best way to install ipub/flysystem is using Composer:
$ composer require ipub/flysystem
After that you have to register extension in config.neon.
extensions:
flysystem: IPub\Flysystem\DI\FlysystemExtension
As a first step is to define your file adapters. Adapters are used for manipulation with files.
flysystem:
adapters:
my_local_adapter:
type: local
directory: %baseDir%/cache
other_adapter:
type: local
directory: %baseDir%/other
Next step for using Flysystem is to create filesystem service, that will handle all your read, write, etc. commands, but at first you have to define adapter.
flysystem:
adapters:
my_local_adapter:
type: local
directory: %baseDir%/cache
other_adapter:
type: local
directory: %baseDir%/other
filesystems:
my_filesystem:
adapter: my_local_adapter
This definition will create new service for you to use:
$filesystem = $container->getService('flysystem.filesystem.my_filesystem');
The naming scheme follows a simple rule: flysystem.filesystem.%s
whereas %s is the name of your filesystem.
The $filesystem
variable is of the type \League\Flysystem\Filesystem
.
Please refer to the General Usage section in the official documentation for details.
All filesystems are automatically mounted to the mount manager service for easy usage. Now, when your filesystem is created, you can just inject mount manager where you need to operate with files:
class FileUploadPresenter extends BasePresenter
{
/**
* @var \League\Flysystem\MountManager
*/
private $mountManager;
/**
* @param \League\Flysystem\MountManager $mountManager
*/
public function __construct(\League\Flysystem\MountManager $mountManager)
{
$this->mountManager = $mountManager;
}
public function handleUpload()
{
//...do some stuff
$this->mountManager->write('my_filesystem://path/where/to/save/filename.ext', $fileContent);
}
}
Details on the usage of the MountManager can be found in the Flysystem documentation.