Hi,
i have problem to save my files in my data base.
the field for take file url in my table is varchar(500).
i am following yii2 documentation i haven't problem but it save one file.
my model: i put
public $files;
[['files'], 'safe'],
[['files'], 'file', 'skipOnEmpty' => false, 'extensions' => 'pdf, gif, jpg, png', 'maxFiles' => 4],
my form : i put this line: <?= $form->field($model, 'files[]')->fileInput(['multiple' => true, 'accept' => 'image/*' ]) ?>
my controller:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$isStudent = Yii::$app->session->get('user_id');
if ($model->load(Yii::$app->request->post())){
$model->files = UploadedFile::getInstances($model, 'files');
foreach ($model->files as $files) {
$save_file = '';
$folder= 'uploads/'.$isStudent.'/localisation/';
if($files){
if(!is_dir($folder)){
mkdir($folder, 0755, true);
}
$model->doc_loc = $folder.rand(10,100).'-'.$files->name;
$model->id_etab = $isStudent;
$model->created_by = Yii::$app->user->id;
$save_file = 1;
}
if($model->save()) {
if($save_file){
//foreach ($model->files as $files) {
$files->saveAs($model->doc_loc);
//}
}
return $this->redirect(['view', 'id' => $model->id_local]);
}
}
}else {
return $this->render('update', [
'model' => $model,
]);
}
}
your code help me to solve my problem! Thank
but i want ti know if you want to delete one of files upload what can you do.
public function actionDeleteimg($id, $field)
{
$img = $this->findModel($id)->$field;
if($img){
if (!unlink($img)) {
return false;
}
}else {
}
$img = $this->findModel($id);
$img->$field = NULL;
$img->update();
return $this->redirect(['update', 'id' => $id]);
}