M
M
Michael2019-01-03 23:38:22
Yii
Michael, 2019-01-03 23:38:22

How to upload a document and an image at the same time?

Hello everybody! Please help me figure it out. In general, the task is this - when forming the material, it is necessary that you can upload a document (doc or zip), as well as upload an image, two different lines are responsible for these two parameters in the form, for a document it is docPath, for a gallery image, sometimes you need to upload only a document, but it happens that only a picture without a document. The functions below are working, but I do not know how to make them work together, the problem is in my code after the validation check.

public function actionCreate()
{
    $model = new Material([
      'dirName' => date('Y-m-d'),
    ]);

    if ($model->load(Yii::$app->request->post())){
        $model->alias = str2url($model->name); // алиас материала
        $model->docFile = UploadedFile::getInstance($model, 'docFile'); // Загрузка документа
        if ($model->validate()) {
            if ($model->save(false)) { // Загрузка картинки
                $model->gallery = UploadedFile::getInstances($model, 'gallery');
                $model->uploadGallery();                    
                Yii::$app->session->setFlash('success', "Материал {$model->name} добавлен");
                return $this->redirect(['view', 'id' => $model->id]);
            }
            if($model->path = $model->upload()) { // Загрузка документа
                if ($model->save(false)) {
                    Yii::$app->session->setFlash('success', "Документ {$model->name} добавлен");
                    return $this->redirect(['view', 'id' => $model->id]);
                }
            }
        }
    }
    return $this->render('create', [
        'model' => $model,
    ]);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Michael, 2019-01-04
@Bally

File _form.php
Upload image

<?= $form->field($model, 'gallery[]')->widget(FileInput::classname(), [
    'name' => 'attachment_49[]',
    'options'=>[
        'multiple'=>true
    ],
]);
?>

Document upload
<?= $form->field($model, 'docFile')->widget(FileInput::classname(), [
    'name' => 'attachment_51',
    'pluginOptions' => [
        'showUpload' => false,
        'browseLabel' => '',
        'removeLabel' => '',
        'mainClass' => 'input-group-lg'
    ],
]) ?>

Model
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;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question