Файл _form.php
Загрузка картинки
<?= $form->field($model, 'gallery[]')->widget(FileInput::classname(), [
'name' => 'attachment_49[]',
'options'=>[
'multiple'=>true
],
]);
?>
Загрузка документа
<?= $form->field($model, 'docFile')->widget(FileInput::classname(), [
'name' => 'attachment_51',
'pluginOptions' => [
'showUpload' => false,
'browseLabel' => '',
'removeLabel' => '',
'mainClass' => 'input-group-lg'
],
]) ?>
Модель
public $gallery;
public $docFile;
public $dirPath = 'upload/documents/';
public $dirName = '';
// Загрузка изображение
public function uploadGallery(){
if($this->validate()){
foreach ($this->gallery as $file){
$path = 'upload/store/' . $file->baseName . '.' . $file->extension;
$file->saveAs($path);
$this->attachImage($path);
@unlink($path);
}
return true;
}else{
return false;
}
}
// Загрузка документа
public function upload()
{
if ($this->validate()) {
$path = $this->processMkDir();
$fileName = Inflector::slug(pathinfo($this->str2url($this->docFile->baseName), PATHINFO_FILENAME)) . '.' . $this->docFile->extension;
$this->docFile->saveAs($path . $fileName);
return $fileName;
} else {
return false;
}
}
protected function processMkDir()
{
$path = (!empty($this->dirName)) ? $this->dirPath.$this->dirName.'/' : $this->dirPath;
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
return $path;
}