Davie's avatar
Level 1

Uploading multiple files using multiple file uploads and storing the names in database

how can I accomplish this in laravel?

0 likes
3 replies
Davie's avatar
Level 1

I can upload multiple files with only one file input the problem is when the input of type file are more than one and both have multiple attribute.

how can we implement this kind of a sytem? https://imgur.com/a/tHFxx

jlrdw's avatar

You have to work up your foreach logic. An example, not your situation, but just an example where I want to upload 1 to 4 (max) images

    public function add() {
        if (isset($_POST['submit'])) {
            $lid = DB::table('recents')->count();
            $k = -1;
            if (empty($lid) || strlen($lid) == 0 || is_null($lid)) {
                $lid = 1;
            }
            $newname = '';
            $destinationPath = ROOTDIR . 'upload/imgrecent/'; /////ADJUST FOR YOUR LARAVEL/////////
            $files = Input::file('ufile');
            //$names = [];
            $arrname = [];
            foreach ($files as $file) {
                $k = $k + 1;
                if (empty($file)) {
                    $arrname[$k] = "";
                } else {
                    $file_name = $file->getClientOriginalName();
                    $file_ext = $file->getClientOriginalExtension();
                    $lid = $lid + $k + 1;
                    $fileInfo = pathinfo($file_name);
                    $filename = $fileInfo['filename'];
                    print_r($filename); /////TAKE OUT WAS FOR TESTING////////////
                    $arrname[$k] = $filename . $lid . "." . $file_ext;
                    $file->move($destinationPath, (string) $arrname[$k]);
                }
            }
            $pic1 = Cln::fixValue((string) $arrname[0]);
            $pic2 = Cln::fixValue((string) $arrname[1]);
            $pic3 = Cln::fixValue((string) $arrname[2]);
            $pic4 = Cln::fixValue((string) $arrname[3]);
            $comments = Cln::fixValue($_POST['comments']);
            if (!isset($error)) {
                $postdata = array(
                    'pic1' => $pic1,
                    'pic2' => $pic2,
                    'pic3' => $pic3,
                    'pic4' => $pic4,
                    'comments' => $comments
                );
                DB::table('recents')->insert($postdata);
            }
        }//end add        

        $this->layout = 'addtpl';
        return View::make('Recents/Add')->shares('title', 'Recent Add');
    }

You would need to foreach twice since you have a different category.

In example change

$pic1 = Cln::fixValue((string) $arrname[0]);

to

$pic1 = (string) $arrname[0];
// etc

Use your names.

Please or to participate in this conversation.