H
H
hollanditkzn2018-02-12 12:14:19
PHP
hollanditkzn, 2018-02-12 12:14:19

How to save multiple entries in db when uploaded files?

I can’t understand why I upload 4 files and see them in the folder and in the records that I displayed, but only 1 record is saved in the database

public function upload($id)
    {
        $attachment = new Image();
        $file = $_FILES['attachment'];
        $fileTempName = $file['tmp_name'];
        foreach ($fileTempName as $key => $tmp){
            if (is_uploaded_file($tmp)){
                $newFilename = __DIR__.'../../web/images/'.time().'-'.$key;
                if ($file['type'][$key] == 'image/png'){
                    $newFilename .= '.png';
                } else if ($file['type'][$key] == 'image/jpg' || $file['type'][$key] == 'image/jpeg'){
                    $newFilename .= '.jpg';
                }
                if (move_uploaded_file($tmp, $newFilename)){
                    echo $newFilename.'<br/>';//Тут я вижу все 4 файла с путями
                    $attachment->path = $newFilename;//Сохраняю путь в бд
                    $attachment->id_question = $id;//связываю id вопроса которая принадлежит файл
                    $attachment->save();//Сохраняю файл
                }
            } else {
                echo 'Файлы не загрузились на сервер';
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stimulate, 2018-02-12
@hollanditkzn

Wangyu $attachment = new Image(); must be run in a loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question