vlaim / yii2-file-upload
Yii2 FileUpload – PHP library for uploading files to local server or Amazon S3
Installs: 2 058
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=5.5.0
- aws/aws-sdk-php: 2.*
- yiisoft/yii2: *
Requires (Dev)
- phpunit/phpunit: 5.5.*
- squizlabs/php_codesniffer: 2.*
This package is not auto-updated.
Last update: 2020-02-21 17:05:57 UTC
README
Yii2 FileUpload – PHP library for uploading files to your server or Amazon S3. It makes easy for developers to handle with yii2 UploadedFile instances. It's also possible to upload files via URLs from external sources.
Getting Started
Installation
The preferred way to install this extension is through composer.
Note: Check the composer.json for this extension's requirements and dependencies. Read this web tip /wiki on setting the
minimum-stability
settings for your application's composer.json.
Either run
$ php composer.phar require vlaim/yii2-file-upload "dev-master"
or add
"vlaim/yii2-file-upload": "dev-master"
to the require
section of your composer.json
file.
Do not forget include Composer autoloader and define namespace for library
<?php require '/path/to/vendor/autoload.php'; use vlaim\fileupload\FileUpload;
Quick Examples
Upload a file to your local server
$photo = UploadedFile::getInstance($model, 'photo'); $uploader = new FileUpload(FileUpload::S_LOCAL);
Upload a file to Amazon S3
This code uploads a file to Amazon S3. You must provide an associative array as second argument in FileUpload constructor in following way:
$photo = UploadedFile::getInstance($model, 'photo'); $uploader = new FileUpload(FileUpload::S_S3, [ 'version' => 'latest', 'region' => '<regiongoeshere>', 'credentials' => [ 'key' => '<keygoeshere>', 'secret' => '<secretgoeshere>' ], 'bucket' => '<bucketgoeshere>' ]);
Methods
setUploadFolder(string $folder)
Sets folder name in which files will be uploaded to.
Default to 'uploads'
$uploader->setUploadFolder('photos');
setFsPath(string $fsPath)
(Only for Local mode)
Sets path in which files will be uploaded to. You can provide absolute or relative path
Default to /
$uploader->setFsPath('/var/www/path/to/your/app/');
setFsUrl(string $url)
(Only for Local mode)
Sets url. For example, if you set path to 'http://static.example.com' file after uploading will have URL http://static.example.com/path/to/your/file
Default to /
$uploader->setFsPath('http://pathtoyoursite.com');
hashFilename(boolean $hash)
Defines if upload filename needs to be hashed using md5 algorythm in following way:
md5($fileName . time() . mt_rand(0, 30) // file.png upload filename will be 2122c3a6ad9997af28cab44b7fe7ab90.jpg
Default to true
$uploader->hashFilename(false);
setACL(string $acl)
Sets Access Control List.
Read more at http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
$uploader->setACL('public-read');
Catching and handling exceptions
To catch exceptions use FileUploadException class
<?php use vlaim\fileupload\FileUploadException; try{ //your code goes here } catch(FileUploadException $e){ echo $e->getMessage(); }
Tests
Will be added soon :)
Issues
Bug reports and feature requests can be submitted on the Github Issue Tracker.
License
yii2-file-upload is released under the MIT License. See the bundled LICENSE.md for details.