1. Go to this page and download the library: Download 68publishers/file-storage library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
68publishers / file-storage example snippets
use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageInterface;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
/** @var Container $container */
$defaultStorage = $container->getByType(FileStorageInterface::class);
$provider = $container->getByType(FileStorageProviderInterface::class);
$defaultStorage = $provider->get();
# or $defaultStorage = $provider->get('default');
$s3storage = $provider->get('s3');
use SixtyEightPublishers\FileStorage\FileStorageInterface;
/** @var FileStorageInterface $storage */
# Create a resource from file or url:
$resource = $storage->createResourceFromFile(
$storage->createPathInfo('test/invoice.pdf'),
__DIR__ . '/path/to/invoice.pdf'
);
$storage->save($resource);
# Create resource from file that is stored in storage:
$resource = $storage->createResource(
$storage->createPathInfo('test/invoice.pdf')
);
# copy to the new location
$storage->save($resource->withPathInfo(
$storage->createPathInfo('test/invoice-2.pdf')
));
use SixtyEightPublishers\FileStorage\FileStorageInterface;
/** @var FileStorageInterface $storage */
if ($storage->exists($storage->createPathInfo('test/invoice.pdf'))) {
echo 'file exists!';
}
use SixtyEightPublishers\FileStorage\FileStorageInterface;
/** @var FileStorageInterface $storage */
$storage->delete($storage->createPathInfo('test/invoice.pdf'));
use SixtyEightPublishers\FileStorage\FileStorageInterface;
/** @var FileStorageInterface $storage */
# /data/files/test/invoice.pdf
echo $storage->link($storage->createPathInfo('test/invoice.pdf'));
# or
$fileInfo = $storage->createFileInfo($storage->createPathInfo('test/invoice.pdf'));
echo $fileInfo->link();
use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Cleaner\StorageCleanerInterface;
/** @var Container $container */
$cleaner = $container->getByType(StorageCleanerInterface::class);
$provider = $container->getByType(FileStorageProviderInterface::class);
$storage = $provider->get('default');
# get files count in the specific namespace:
$cleaner->getCount($storage->getFilesystem(), [
StorageCleanerInterface::OPTION_NAMESPACE => 'test',
]);
# get files count in the whole storage:
$cleaner->getCount($storage->getFilesystem());
# remove files in the specific namespace:
$cleaner->clean($storage->getFilesystem(), [
StorageCleanerInterface::OPTION_NAMESPACE => 'test',
]);
# clean the whole storage:
$cleaner->clean($storage->getFilesystem());
use Nette\DI\Container;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Asset\AssetsCopierInterface;
/** @var Container $container */
$copier = $container->getByType(AssetsCopierInterface::class);
$provider = $container->getByType(FileStorageProviderInterface::class);
# Copies assets defined in the configuration
$copier->copy($provider->get('default'));
$copier->copy($provider->get('s3'));
use Nette\DI\CompilerExtension;
use SixtyEightPublishers\FileStorage\Bridge\Nette\DI\Assets;
use SixtyEightPublishers\FileStorage\Bridge\Nette\DI\AssetsProviderInterface;
final class MyCompilerExtension extends CompilerExtension implements AssetsProviderInterface
{
public function provideAssets() : array
{
return [
new Assets('s3', [
'path/to/file1.jpeg' => 'namespace/file1.jpeg',
'path/to/file2.jpeg' => 'namespace/file2.jpeg',
]),
];
}
}
use Doctrine\DBAL\Types\Type;
use SixtyEightPublishers\FileStorage\FileStorageProviderInterface;
use SixtyEightPublishers\FileStorage\Bridge\Doctrine\DbalType\FileInfoType;
/** @var FileStorageProviderInterface $fileStorageProvider */
Type::addType(FileInfoType::NAME, FileInfoType::class);
# this line is important:
Type::getType(FileInfoType::NAME)->setFileStorageProvider($fileStorageProvider);
use Doctrine\ORM\Mapping as ORM;
use SixtyEightPublishers\FileStorage\FileInfoInterface;
/**
* @ORM\Entity
*/
class File
{
# ID and other columns
/**
* @ORM\Column(type="file_info")
*/
private FileInfoInterface $source;
public function __construct(FileInfoInterface $source)
{
$this->source = $source;
}
public function getSource(): FileInfoInterface
{
return $this->source;
}
}