bboure / yii2-upload-from-url

UploadFromUrl extension for Yii 2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UploadFromUrl extension for Yii 2

Build Status Latest Stable Version Total Downloads License Dependency Status codecov.io

This is the upload file from url address extension for Yii 2. It have class UploadFromUrl for upload files from URL and it have FileFromUlrValidator for validate model attribute with file from url.

Please submit issue reports and pull requests to the main repository. For license information check the LICENSE-file.

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist igogo5yo/yii2-upload-from-url

or add

"igogo5yo/yii2-upload-from-url": ">=1.0"

to your composer.json file

Usage

Example 1

$model = new Post();
$model->load(Yii::$app->request->post());

$file = UploadFromUrl::getInstance($model, 'image');

//if second parameter is TRUE it writes uploaded file path to this model property
$file->saveAs('uploads/yii.png', true);   

echo $model->image; // uploads/yii.png

Example 2

$model = new Post();
$model->image = 'http://static.yiiframework.com/files/logo/yii.png';

$file = UploadFromUrl::initWithModel($model, 'image');

//if second parameter is TRUE it writes uploaded file path to this model property
$file->saveAs('uploads/yii.png', true);   

echo $model->image; // uploads/yii.png

Example 3

$url = 'http://static.yiiframework.com/files/logo/yii.png' ;
$path = 'uploads/yii.png';

$file = UploadFromUrl::initWithUrl($url);
$file->saveAs($path);   

//Set to model
$model = new Post();
$model->image = $path;

Example 4

$url = 'http://static.yiiframework.com/files/logo/yii.png' ;
$path = 'uploads/yii.png';
$model = new Post();

$file = UploadFromUrl::initWithUrlAndModel($url, $model, 'image');
$file->saveAs($path, true);   

echo $model->image; // uploads/yii.png

Validation Example

[
...
     [['image'], 'igogo5yo\uploadfromurl\FileFromUrlValidator', 'extensions' => 'csv', 'mimeTypes' => 'text/plain'],
...
]

Run Tests

phpunit --bootstrap tests/bootstrap.php tests

About

UploadFromUrl extension for Yii 2

License:MIT License


Languages

Language:PHP 100.0%