qianlizeguo/yii2-mall-storage

There is no license information available for the latest version (dev-master) of this package.

A file uploading extension for Yii2, saving files to object storage service.

dev-master 2019-06-06 09:34 UTC

This package is not auto-updated.

Last update: 2024-05-04 08:11:14 UTC


README

Usage

  1. Firstly, add this lines of code to your Yii application config:

    'components' => [
        'class' => 'Hejiang\Storage\Components\StorageComponent',
        'basePath' => 'temp/',
        'driver' => [
            'class' => 'Hejiang\Storage\Drivers\Local',
            'accessKey' => '',
            'secretKey' => '',
            'bucket' => '',
        ]
    ]
  2. Then after app bootstarpping, you would get the storage component instance like that:

    $storage = \Yii::$app->storage;

    Alternatively, you can also create a driver while app running:

    $storage->setDriver('Hejiang\Storage\Drivers\Local', []);
  3. Fetch uploaded file by field name:

    $file = $storage->getUploadedFile('FILE-FIELD-NAME');
  4. Save it.

    $url = $file->saveAs('NEW-FILE-NAME.EXT');
    // or
    $url = $file->saveWithOriginalExtension('NEW-FILE-BASE-NAME');
    // or
    $url = $file->saveAsUniqueHash();

    $url will be a URL string which can access this file on success, or false on failure.

    If there's any error occurred, these methods will throw a Hejiang\Storage\Exceptions\StorageException. Don't forget to try... catch ....

About