<commit_before>UploadFromUrl extension for Yii 2
=====================================
[![Latest Stable Version](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/v/stable)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Total Downloads](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/downloads)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Latest Unstable Version](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/v/unstable)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![License](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/license)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Dependency Status](https://www.versioneye.com/user/projects/550c2c20a80b5fc12d000271/badge.svg?style=flat)](https://www.versioneye.com/user/projects/550c2c20a80b5fc12d000271)

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](LICENSE.md)-file.

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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**
```php
$model = new Post();
$model->image = 'http://static.yiiframework.com/files/logo/yii.png';

$file = UploadFileByURL::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 2**
```php
$url = 'http://static.yiiframework.com/files/logo/yii.png' ;
$path = 'uploads/yii.png';

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

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


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

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

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

**Validation Example**
```php
[
...
     [['image'], 'igogo5yo\uploadfromurl\FileFromUrlValidator', 'extensions' => 'csv', 'mimeTypes' => 'text/plain'],
...
]
```
<commit_msg>upd description<commit_after>UploadFromUrl extension for Yii 2
=====================================
[![Latest Stable Version](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/v/stable)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Total Downloads](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/downloads)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Latest Unstable Version](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/v/unstable)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![License](https://poser.pugx.org/igogo5yo/yii2-upload-from-url/license)](https://packagist.org/packages/igogo5yo/yii2-upload-from-url) [![Dependency Status](https://www.versioneye.com/user/projects/550c2c20a80b5fc12d000271/badge.svg?style=flat)](https://www.versioneye.com/user/projects/550c2c20a80b5fc12d000271)

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](LICENSE.md)-file.

Installation
------------

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

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**
```php
$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 2**
```php
$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 3**
```php
$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**
```php
[
...
     [['image'], 'igogo5yo\uploadfromurl\FileFromUrlValidator', 'extensions' => 'csv', 'mimeTypes' => 'text/plain'],
...
]
```

{'fraction_non_alphanumeric': 0.19357995693873556, 'fraction_numerical': 0.024270894499902135, 'mean_word_length': 4.754504504504505, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 20, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '435262', 'n_tokens_mistral': 1922, 'n_tokens_neox': 1800, 'n_words': 347}