避免在上传文件时出错的最佳方法
Best way to avoid getting error in uploading a file
PHP Warning – yii\base\ErrorException
move_uploaded_file(/doc_2345.txt): failed to open stream: Permission
denied
-这是我尝试上传文件并将其保存到数据库时的错误 (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,
]);
}
}
请更改文件夹权限以及 "filesPath" 文件夹中的所有其他文件和文件夹。
$file -> saveAs(Yii::getAlias('@webroot/filesPath') . '/' . $docuName);
我想它会解决你的问题。
PHP Warning – yii\base\ErrorException
move_uploaded_file(/doc_2345.txt): failed to open stream: Permission denied
-这是我尝试上传文件并将其保存到数据库时的错误 (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,
]);
}
}
请更改文件夹权限以及 "filesPath" 文件夹中的所有其他文件和文件夹。
$file -> saveAs(Yii::getAlias('@webroot/filesPath') . '/' . $docuName);
我想它会解决你的问题。