PHP code example of bestyii / yii2-aliyun-oss

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

    

bestyii / yii2-aliyun-oss example snippets


return [
    //...
    'components' => [
        //...
        'fs' => [
                    'class' => 'bestyii\aliyunoss\Filesystem',
                    'accessId' => OSS_ACCESS_ID,
                    'accessSecret' => OSS_ACCESS_SECRET,
                    'region' => OSS_REGION,
                    'bucket' => OSS_BUCKET,
                    'endpoint' => OSS_ENDPOINT,
                    // 'timeout'        => 3600,
                    // 'connectTimeout' => 10,
                    // 'isCName'        => false,
                    // 'token'          => '',
                ],
    ],
];

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

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

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

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

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

$stream = fopen('/path/to/somefile.ext', 'r+');
Yii::$app->fs->putStream('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->has('filename.ext');

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

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

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

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

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

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

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

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

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

use League\Flysystem\AdapterInterface;

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

use League\Flysystem\AdapterInterface;

if (Yii::$app->fs->getVisibility('filename.ext') === AdapterInterface::VISIBILITY_PRIVATE) {
    Yii::$app->fs->setVisibility('filename.ext', AdapterInterface::VISIBILITY_PUBLIC);
}

$contents = Yii::$app->fs->listContents();

foreach ($contents as $object) {
    echo $object['basename']
        . ' is located at' . $object['path']
        . ' and is a ' . $object['type'];
}

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

$paths = Yii::$app->fs->listPaths();

foreach ($paths as $path) {
    echo $path;
}

$listing = Yii::$app->fs->listWith(
    ['mimetype', 'size', 'timestamp'],
    'optional/path/to/directory',
    true
);

foreach ($listing as $object) {
    echo $object['path'] . ' has mimetype: ' . $object['mimetype'];
}

$info = Yii::$app->fs->getWithMetadata('path/to/filename.ext', ['timestamp', 'mimetype']);
echo $info['mimetype'];
echo $info['timestamp'];