A
A
Alexander Ivanov2018-03-04 16:05:56
Yii
Alexander Ivanov, 2018-03-04 16:05:56

Why is the file not loading from the form to the correct directory?

There is a form with which the data is loaded into the database and displayed on another page. You need to add a file upload to the form.
The form

<?php

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

use yii\helpers\Html;
use yii\widgets\ActiveForm;
$status = Yii::$app->user->identity->status;
?>
<div>
<? if($status == 'admin'){ ?>
<?php $form = ActiveForm::begin([ 'method' => 'POST','id' => 'addproject-form', 'options' => ['enctype' => 'multipart/form-data']]) ?>
<?= $form->field($form_model, 'name') ?>
<?= $form->field($form_model, 'url') ?>
<?= $form->field($form_model, 'resurs') ?>
<?= $form->field($form_model, 'studio') ?>
<?= $form->field($form_model, 'pic')->fileInput() ?>
<?= Html::submitButton('Добавить', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end() ?>
<? }
?>
</div>

Controller
public function actionAddproject()
    {
    $this->enableCsrfValidation = false;
    
    $form_model = new ProjectAddForm();
        if($form_model->load(\Yii::$app->request->post())){
          
          $name_form = $_POST['ProjectAddForm']['name'];
          $url_form = $_POST['ProjectAddForm']['url'];
          $resurs_form = $_POST['ProjectAddForm']['resurs'];
          $studio = $_POST['ProjectAddForm']['studio'];
          $pic = $_FILES['ProjectAddForm']['name']['pic'];
          
            $form_model -> pic = UploadedFile::getInstance($form_model,'pic');		
                 $form_model -> pic -> saveAs('projects/' .$pic);

            $date = new Projects();

      
             $date ->name = $name_form;
             $date ->url = $url_form;
             $date ->resurs = $resurs_form;
             $date ->studio = $studio;
             $date ->pic = $pic;
             $date->save();			
        }	
        return $this->render('addproject', compact('form_model'));	
    }

The data is successfully saved to the database, but the file itself is not saved to the correct folder.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question