我正在使用Yii2和Kartik的FileInput扩展程序。我遇到了保存到目录的问题。我正在使用yii-advanced模板,并使用gii生成CRUD代码。目前正在编辑它。
当我保存它时,它拒绝保存并显示"请上传文件"消息。
主要使用.doc和.pdf保存的文件。
如果我设置了' skipOnEmpty = True'在模型中,它将保存表单,但不保存文件。
我的mysql表上的数据类型设置为VARCHAR,因为我只想保存它的路径。
我遵循了本指南 - http://webtips.krajee.com/upload-file-yii-2-using-fileinput-widget/
这是我的模特;
<?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'],
];
}
}
然后这是我的控制器(我只包括上传功能,但剩下的就是生成的代码(CRUD动作),
<?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,
]);
}
}
这是我的观点,也和Controller一样。代码只是上传部分。
<?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>
希望我提供了足够的信息。感谢任何帮助。
编辑:这是日志的样子,我认为它是验证问题,但我无法确定哪一个导致它,模型或控制器。答案 0 :(得分:2)
检查路径是否正确并且不缺少/例如。
定义您的路径:
Yii::$app->params['uploadPath'] = Yii::getAlias("@frontend") . '/web/uploads/';
然后, $ model-&gt; fm_upload 在保存之前没有值:
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
}
}