Reputation: 249
i am using Yii2 and Kartik's FileInput extension. and I am having issues with it saving to the directory. I am using the yii-advanced template, and generate the CRUD codes using gii. Currently editing it as I go.
When I save it it refused to save and show the "Please upload a file" message.
Files that will be saved with be .doc and .pdf mostly.
If I set the 'skipOnEmpty = True' in the model, it will save the form but not the file.
Datatype on my mysql table is set to VARCHAR because I want to save its path only.
I followed this guide - http://webtips.krajee.com/upload-file-yii-2-using-fileinput-widget/
Here is my model;
<?php
namespace app\models;
use Yii;
class FormMovement extends \yii\db\ActiveRecord
{
public static function tableName()
{
return 'form_movement';
}
public function rules()
{
return [
[['fm_date_received', 'fm_form_name', 'fm_from', 'fm_ptj'], 'required'],
[['form_id'], 'integer'],
[['fm_date_received', 'fm_date_action1', 'fm_date_action2'], 'safe'],
[['fm_form_name', 'fm_note'], 'string', 'max' => 500],
[['fm_from', 'fm_ptj', 'fm_action1', 'fm_action2'], 'string', 'max' => 100],
[['fm_upload'], 'file', 'skipOnEmpty' => false, 'extensions'=>'jpg,pdf,png,doc,docx,xls,xlsx'],
];
}
}
Then here is my controller(i included only the upload function, but it is the rest with the generated codes(CRUD actions),
<?php
namespace frontend\controllers;
use Yii;
use app\models\FormMovement;
use app\models\SearchFormMovement;
//use app\models\FormType;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
class FormMovementController extends Controller
{
public function actionUpload()
{
$model = new FormMovement;
if ($model->load(Yii::$app->request->post())) {
$file = UploadedFile::getInstance($model, 'fm_upload');
// store the source file name
$model->fm_upload = $file->name;
$ext = end((explode(".", $file->name)));
$model->fm_upload = Yii::$app->security->generateRandomString().".{$ext}";
$path = Yii::$app->params['uploadPath'] . $model->fm_upload;;
if($model->save()){
$file->saveAs($path);
return $this->redirect(['view', 'id'=>$model->_id]);
} else {
// error in saving model
}
}
return $this->render('create', [
'model'=>$model,
]);
}
}
and this is my view, also the same like Controller. Codes are only the upload part.
<?php
use yii\helpers\Html;
use kartik\date\DatePicker;
//use app\models\FormMovement;
use app\models\FormType;
use yii\bootstrap\ActiveField;
use kartik\form\ActiveForm;
use kartik\file\FileInput;
/* @var $this yii\web\View */
/* @var $model app\models\FormMovement */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="form-movement-form">
<?php $form = ActiveForm::begin([![\[][1]][1]
'type' => ActiveForm::TYPE_HORIZONTAL,
'formConfig' => ['labelSpan' => 3, 'deviceSize' => ActiveForm::SIZE_SMALL],
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<div class="form-group kv-fieldset-inline">
<?= Html::activeLabel($model, 'fm_upload', [
'label'=>'MUAT NAIK FAIL',
'class'=>'col-sm-1 control-label'
]) ?>
<div class="col-sm-8">
<?= $form->field($model, 'fm_upload',[
'showLabels'=>false
])->widget(FileInput::classname(), [
'options' => ['accept' => 'image/*'],
'pluginOptions'=>['allowedFileExtensions'=>['jpg','pdf','png','doc','docx','xls','xlsx']
]]) ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
Hope I have provided enough info. Appreciate any help.
EDIT: Here is what the logs looks like, I think its the validation issue but I can't really figure out which one is causing it, the model or the controller.
Upvotes: 0
Views: 2786
Reputation:
Check the path is correct and doesn't lack of / for example.
Define your path:
Yii::$app->params['uploadPath'] = Yii::getAlias("@frontend") . '/web/uploads/';
Then, $model->fm_upload doesn't have the value until you save it:
if ($model->load(Yii::$app->request->post())) {
$file = UploadedFile::getInstance($model, 'fm_upload');
// store the source file name
$ext = end((explode(".", $file->name)));
$new_name = Yii::$app->security->generateRandomString().".{$ext}";
$path = Yii::$app->params['uploadPath'] . '/' . $new_name;
$model->fm_upload = $new_name;
$file->saveAs($path);
if($model->save()){
return $this->redirect(['view', 'id'=>$model->_id]);
} else {
// error in saving model
}
}
Upvotes: 2