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.
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;
$photo = UploadedFile::getInstance($model, 'photo');
$uploader = new FileUpload(FileUpload::S_LOCAL);
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>'
]);
Sets folder name in which files will be uploaded to.
Default to 'uploads'
$uploader->setUploadFolder('photos');
(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/');
(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');
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);
Sets Access Control List.
Read more at http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
$uploader->setACL('public-read');
To catch exceptions use FileUploadException class
<?php
use vlaim\fileupload\FileUploadException;
try{
//your code goes here
}
catch(FileUploadException $e){
echo $e->getMessage();
}
Will be added soon :)
Bug reports and feature requests can be submitted on the Github Issue Tracker.
yii2-file-upload is released under the MIT License. See the bundled LICENSE.md for details.
Comments