igogo5yo/yii2-upload-from-url

UploadFromUrl extension for Yii Framework 2

Installs: 67 972

Dependents: 0

Suggesters: 0

Security: 0

Stars: 8

Watchers: 5

Forks: 6

Open Issues: 0

Type:yii2-extension

v1.4 2018-01-16 07:37 UTC

This package is not auto-updated.

Last update: 2024-11-09 18:06:31 UTC


README

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