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.
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
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.pngExample 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.pngExample 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.pngValidation Example
[
...
[['image'], 'igogo5yo\uploadfromurl\FileFromUrlValidator', 'extensions' => 'csv', 'mimeTypes' => 'text/plain'],
...
]phpunit --bootstrap tests/bootstrap.php tests