PHP code example of acidwave / yii2-flysystem

1. Go to this page and download the library: Download acidwave/yii2-flysystem 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/ */

    

acidwave / yii2-flysystem example snippets


return [
    //...
    'components' => [
        //...
        'fs' => [
            'class' => 'Acidwave\Flysystem\LocalFilesystem',
            'path' => '@webroot/files',
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'ftpFs' => [
            'class' => 'Acidwave\Flysystem\FtpFilesystem',
            'host' => 'ftp.example.com',
            // 'port' => 21,
            'username' => 'your-username',
            'password' => 'your-password',
            // 'ssl' => true,
            // 'timeout' => 60,
            'root' => '/path/to/root',
            // 'passive' => false,
            // 'transferMode' => FTP_TEXT,
            // 'enableTimestampsOnUnixListings' => false,
            // 'systemType' => null, // 'windows' or 'unix'
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'awss3Fs' => [
            'class' => 'Acidwave\Flysystem\AwsS3Filesystem',
            'key' => 'your-key',
            'secret' => 'your-secret',
            'bucket' => 'your-bucket',
            'region' => 'your-region',
            // 'version' => 'latest',
            // 'baseUrl' => 'your-base-url',
            // 'prefix' => 'your-prefix',
            // 'visibility' => \League\Flysystem\Visibility::PUBLIC,
            // 'endpoint' => 'http://my-custom-url'
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'dropboxFs' => [
            'class' => 'Acidwave\Flysystem\DropboxFilesystem',
            'token' => 'your-token',
            // 'prefix' => 'your-prefix',
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'sftpFs' => [
            'class' => 'Acidwave\Flysystem\SftpFilesystem',
            'host' => 'sftp.example.com',
            // 'port' => 22,
            'username' => 'your-username',
            'password' => 'your-password',
            'privateKey' => '/path/to/or/contents/of/privatekey',
            'passphrase' => 'my-super-secret-passphrase-for-the-private-key',
            // 'useAgent' => false,
            // 'timeout' => 60,
            'root' => '/path/to/root',
            // 'dirPrivate' => 0700,
            // 'dirPublic' => 0755,
            // 'filePrivate' => 0600,
            // 'filePublic' => 0644,
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'ziparchiveFs' => [
            'class' => 'Acidwave\Flysystem\ZipArchiveFilesystem',
            'filename' => '@webroot/files/archive.zip',
            // 'localDirectoryPermissions' => 700,
        ],
    ],
];

return [
    //...
    'components' => [
        //...
        'fsID' => [
            //...
            'config' => [
                'visibility' => \League\Flysystem\Visibility::PRIVATE,
            ],
        ],
    ],
];

Yii::$app->fs->write('filename.ext', 'contents');

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->writeStream('filename.ext', $stream);

$contents = Yii::$app->fs->read('filename.ext');

$stream = Yii::$app->fs->readStream('filename.ext');
$contents = stream_get_contents($stream);
fclose($stream);

$exists = Yii::$app->fs->fileExists('filename.ext');

Yii::$app->fs->delete('filename.ext');

Yii::$app->fs->move('filename.ext', 'newname.ext');

$mimetype = Yii::$app->fs->mimeType('filename.ext');

$timestamp = Yii::$app->fs->lastModified('filename.ext');

$timestamp = Yii::$app->fs->fileSize('filename.ext');

Yii::$app->fs->createDirectory('path/to/directory');

Yii::$app->fs->write('path/to/filename.ext');

Yii::$app->fs->deleteDirectory('path/to/filename.ext');

use League\Flysystem\Visibility;

Yii::$app->fs->write('filename.ext', 'contents', [
    'visibility' => Visibility::PRIVATE
]);

use League\Flysystem\Visibility;

if (Yii::$app->fs->visibility('filename.ext') === Visibility::PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', Visibility::PUBLIC);
}

$contents = Yii::$app->fs->listContents('path/to/directory');

foreach ($contents as $object) {
    echo basename($object->path())
        . ' is located at' . $object->path()
        . ' and is a ';
    if ($object instanceof \League\Flysystem\FileAttributes) {
        echo 'file' . PHP_EOF;
    } elseif ($object instanceof \League\Flysystem\DirectoryAttributes) {
        echo 'directory' . PHP_EOF;
    }
}

$contents = Yii::$app->fs->listContents('path/to/directory', true);