PHP code example of pollen-solutions / filesystem

1. Go to this page and download the library: Download pollen-solutions/filesystem 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/ */

    

pollen-solutions / filesystem example snippets


use Pollen\Filesystem\StorageManager;

$storage = new StorageManager();

try {
    $listing = $storage->listContents('/');

    /** @var \League\Flysystem\StorageAttributes $item */
    foreach ($listing as $item) {
        $path = $item->path();

        if ($item instanceof \League\Flysystem\FileAttributes) {
            var_dump($path);
        } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) {
            var_dump($path);
        }
    }
} catch (\League\Flysystem\FilesystemException $e) {
    var_dump($e->getMessage());
}


use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */

// Gets a filesystem instance provided by the storage manager from its name identifier.
$storage->disk('my-own-disk');

// Writing files in default filesystem.
try {
    $storage->write($path, $contents, $config);
} catch (\League\Flysystem\FilesystemException | \League\Flysystem\UnableToWriteFile $exception) {
    // handle the error
}

// Reading files in default filesystem.
try {
    $response = $storage->read($path);
} catch (FilesystemException | UnableToReadFile $exception) {
    // handle the error
}

// And more ...
// @see https://flysystem.thephpleague.com/v2/docs/usage/filesystem-api/

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$defaultDisk = $storage->createLocalFilesystem('/my/secured/fallback/directory/absolute_path');

$storage->setDefaultDisk($defaultDisk);

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$storage->registerLocalDisk('my-local-disk', '/my/local/directory/absolute_path');

use Pollen\Filesystem\LocalFilesystem;
use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$filesystem = new LocalFilesystem($storage->createLocalAdapter('/my/local/directory/absolute_path'));

$storage->addDisk('my-local-disk', $filesystem);

use Pollen\Filesystem\LocalFilesystemAdapter;
use Pollen\Filesystem\LocalFilesystem;
use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$adapter = new LocalFilesystemAdapter('/my/local/directory/absolute_path');
$filesystem = new LocalFilesystem($adapter);

$storage->addDisk('my-local-disk', $localDisk);

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$disk = $storage->registerLocalDisk('my-local-disk', '/my/local/directory/absolute_path');

// Returns the HTTP response to download a file.
$disk->downloadResponse('/sample.txt');

// Returns the HTTP binary file response to download a file.
$disk->binaryFileResponse('/sample.txt');

// Returns the url of a file or a directory.
$disk->getUrl('/sample.txt')

// Gets the absolute path of a file or a directory.
$disk->getAbsolutePath('/sample.txt');

// Gets the SplFileInfo instance of a file or a directory.
$disk->getSplFileInfo('/sample.txt');

// Gets the storage file attributes of a file or a directory.
$disk->getStorageAttributes('/sample.txt');

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$filesystem = $storage->registerLocalImageDisk('my-image-disk', '/my/image/directory/absolute_path'));

use Pollen\Filesystem\LocalImageFilesystem;
use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$filesystem = new LocalImageFilesystem($storage->createLocalAdapter('/my/image/directory/absolute_path'));

$storage->addLocalDisk('my-image-disk', $filesystem);

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$disk = $storage->registerLocalImageDisk('my-image-disk', '/my/image/directory/absolute_path'));

// Gets the HTML render of an image file from its path.
$disk->HtmlRender('/sample.jpg');

// Gets the image file url from its path.
$disk->getImgSrc('/sample.jpg');

use Pollen\Filesystem\StorageManagerInterface;

/** @var StorageManagerInterface $storage */
$disk = $storage->registerS3Disk(
    's3-disk',
    [
        'version'     => 'latest',
        'region'      => 'us-east-2',
        'endpoint'    => '{{ my-s3-endpoint }}',
        'credentials' => [
            'key'    => '{{ my-s3-user }}',
            'secret' => '{{ my-s3-user-password }}',
        ]
    ],
    'my-aws-s3-bucket-name'
);

if ($disk = $storage->disk('s3-disk')) {
    try {
        $listing = $disk->listContents('/';

        /** @var \League\Flysystem\StorageAttributes $item */
        foreach ($listing as $item) {
            $path = $item->path();

            if ($item instanceof FileAttributes) {
                var_dump($path);
            } elseif ($item instanceof DirectoryAttributes) {
                var_dump($path);
            }
        }
    } catch (FilesystemException $e) {
        var_dump($e->getMessage());
    }
}

use Pollen\Filesystem\Filesystem;
use Pollen\Filesystem\StorageManagerInterface;

// The internal adapter
$adapter = new \League\Flysystem\Ftp\FtpAdapter(
    // Connection options
    \League\Flysystem\Ftp\FtpConnectionOptions::fromArray([
        'host' => 'hostname', // sferMode' => FTP_BINARY,
        'systemType' => null, // 'windows' or 'unix'
        'ignorePassiveAddress' => null, // true or false
        'timestampsOnUnixListingsEnabled' => false, // true or false
        'recurseManually' => true // true 
    ])
);

$filesystem = new Filesystem($adapter);

/** @var StorageManagerInterface $storage */
$storage->addDisk('ftp-disk', $filesystem);

if ($disk = $storage->disk('ftp-disk')) {
    try {
        $listing = $disk->listContents('/');
    
        /** @var \League\Flysystem\StorageAttributes $item */
        foreach ($listing as $item) {
            $path = $item->path();
    
            if ($item instanceof \League\Flysystem\FileAttributes) {
                var_dump($path);
            } elseif ($item instanceof \League\Flysystem\DirectoryAttributes) {
                var_dump($path);
            }
        }
    } catch (\League\Flysystem\FilesystemException $e) {
        var_dump($e->getMessage());
    }
}

use Pollen\Filesystem\StorageManagerInterface;
use Pollen\Filesystem\FilesystemDriver;
use League\Flysystem\Ftp\FtpAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;

$driver = new class extends FilesystemDriver
{
    protected ?string $adapterDefinition = FtpAdapter::class

    /**
     * @inheritDoc
     *
     * {@internal Allows to pass and parse an array of connection options.}
     */
    public function parseArgs(...$args) : array{
        if (!isset($args[0]) || !is_array($args)) {
            throw new RuntimeException('FTP Filesystem first argument could be an array of connection options.');
        }

        $config = $args[0];
        $config['host'] = $config['host']?? '127.0.0.1';
        $config['root'] = $config['root']?? '/';
        $config['username'] = $config['username'] ?? 'root';
        $config['password'] = $config['password'] ?? 'root';

        $_args[] = FtpConnectionOptions::fromArray($config);

        return $_args;
    }
};

/** @var StorageManagerInterface $storage */
$storage->registerDriver('ftp', $driver);

$storage->registerDisk('ftp-disk', 'ftp', [
    'host'      => 'hostname', //