2017-05-08 132 views
-2

PHP的警告 - 警予\基地\ ErrorExceptionYii2錯誤文件上傳

move_uploaded_file(/doc_2345.txt):未能打開流:權限被拒絕

- 這是錯誤當我嘗試上傳的文件並將其保存到數據庫(MYSQL)。 我是使用框架創建網站的新手。所以我不知道如何解決它。

_form。

<div class="documents-form"> 

<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> 
<?= $form->field($model, 'reference_no')->textInput() ?> 
<?= $form->field($model, 'subject')->textInput(['maxlength' => true]) ?> 
<?= $form->field($model, 'doc_date')->textInput() ?> 
<?= $form->field($model, 'doc_for')->textInput(['maxlength' => true]) ?> 
<?= $form->field($model, 'doc_from')->textInput(['maxlength' => true]) ?> 
<?= $form->field($model, 'drawer_id')->textInput(['maxlength' => true]) ?> 
<?= $form->field($model, 'doc_file')->fileInput() ?> 
<div class="form-group"> 
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> 
</div> 

<?php ActiveForm::end(); ?> 

模型。

public $file; 
public static function tableName() 
{ 
    return 'documents'; 
} 

/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     [['reference_no', 'subject', 'doc_date', 'doc_for', 'drawer_id','doc_from', 'doc_file'], 'required'], 
     [['reference_no'], 'integer'], 
     [['doc_date'], 'safe'], 
     [['subject', 'doc_for', 'drawer_id','doc_from'], 'string', 'max' => 250], 
     [['doc_file'], 'string', 'max' => 300], 
    ]; 
} 

控制器。

public function actionCreate() 
{ 
    $model = new Documents(); 

    if ($model->load(Yii::$app->request->post())) { 
     $model->save(); 
     $docuId = $model->reference_no; 
     $file = UploadedFile::getInstance($model, 'doc_file'); 
     $docuName = 'doc_' . $docuId . '.' . $file->getExtension(); 
     $file -> saveAs(Yii::getAlias('@webroot/filesPath') . '/' . $docuName); 
     $model -> doc_file = $docuName; 
     $model -> save(); 

     return $this -> redirect(['view', 'id' => $model->id]); 

    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 
+0

'@ webroot/filesPath'是否存在且可寫? – Chux

回答

1

請更改文件夾權限和所有其他文件&文件夾中的「filesPath」文件夾。

$file -> saveAs(Yii::getAlias('@webroot/filesPath') . '/' . $docuName); 

我認爲這會解決您的問題。

+0

謝謝。現在工作:D –