mardi 18 juillet 2017

laravel upload files when update

when update many input files, upload the old file not deleted and add new file and i used \File::delete

    $model = Project::findOrFail($id);
            $model->update($request->all());
            $files = $request->all();
                foreach ($files as $fieldName => $file) {
                    if ($fieldName == 'file1' && !empty($file)) {
                        $destinationPath = public_path() . '/uploads';
                        $extension = $file->getClientOriginalExtension();
                        $filename = uniqid() . '.' . $extension;
                        $file->move($destinationPath, $filename);
                        \File::delete(public_path() . '/uploads' . $model->file1);
                        $model->file1 = $filename;

                    }
                    if ($fieldName == 'file2' && !empty($file)) {
                        $destinationPath = public_path() . '/uploads';
                        $extension = $file->getClientOriginalExtension();
                        $filename = uniqid() . '.' . $extension;
                        $file->move($destinationPath, $filename);
                        \File::delete(public_path() . '/uploads/' . $model->file2);
                        $model->file2 = $filename;

        }
                      $model->update();



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2u5CnKX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire