nazgyl's avatar
Level 15

Multiple file upload saving only 1 file

Hello guys, i'm missing something here and cant figure it out!

the code below handles multiple file uploads; however, it only stores one file on the server, data is inserted in the database for all files including their unique hashes correctly, except for $tab->location ... it always points to the location of the only file that was stored on the server.

Any help is appreciated!

blade

<input type="file" class="custom-file-input form-control form-control-sm" name="files[]" multiple>

controller

 if($request->hasfile('files')) {

            foreach($request->file('files') as $file) {
                $fileName = $request->bid . '.' . $request->aid . '.' . $request->tid . '.' . Auth::user()->id . '.' . time() . '.' . $file->getClientOriginalExtension();
                $file->storeAs(date('Y') . '/' . date('m'), $fileName, 'tabs');
                $tabfile = '/assets/tabs/' . date('Y') . '/' . date('m') . '/' . $fileName;
                $album = Album::find($request->aid);
                $track = Track::find($request->tid);
                $band = Band::find($request->bid);
                    $tab = new Tab;
                    $tab->name = $request->tabname;
                    $tab->location = date('Y') . '/' . date('m') . '/' . $fileName;
                    //gp,gp3,gp4,gp5
                    if ($request->tabtype == 0){
                    if ($file->getClientOriginalExtension() == "gpx" or $file->getClientOriginalExtension() == "gp" or $file->getClientOriginalExtension() == "gp3" or $file->getClientOriginalExtension() == "gp4" or $file->getClientOriginalExtension() == "gp5" or $file->getClientOriginalExtension() == "gtp") {
                        $tab->tab_types_id = 4;
                    }
                    elseif ($file->getClientOriginalExtension() == "ptb")
                    {
                        $tab->tab_types_id = 2;
                    }
                    else
                    {
                        if(strpos($file->getClientOriginalName(), '_BT.txt') !== false){
                            $tab->tab_types_id = 3;
                        }
                        elseif(strpos($file->getClientOriginalName(), '_DT.txt') !== false)
                        {
                            $tab->tab_types_id = 6;
                        }
                        elseif(strpos($file->getClientOriginalName(), '_KT.txt') !== false)
                        {
                            $tab->tab_types_id = 5;
                        }
                        else
                            {
                              $tab->tab_types_id = 1;
                        }

                    }
                    $tab->status = 0;
                    $tab->tracks_id = $request->tid;
                    $tab->album_id = $request->aid;
                    $tab->band_id = $request->bid;
                    $tab->band_mbid = $band->musicbrainz;
                    $tab->album_mbid = $album->musicbrainz;
                    $tab->tracks_mbid = $track->musicbrainz;
                    $tab->user_id = Auth::user()->id;
                    $tab->tab_hash = hash_file('sha256', public_path($tabfile));
                    $tab->save();
                }

            }

            return redirect()->route('album.show', ['slug' => $album->slug]);




            }

        }
0 likes
2 replies
mvd's avatar

Hi @nazgyl

I guess (can't see the other form fields), the filename is always the same?

$fileName = $request->bid . '.' . $request->aid . '.' . $request->tid . '.' . Auth::user()->id . '.' . time() . '.' . $file->getClientOriginalExtension();
Snapey's avatar

Do you expect that your query is so slow that some time will have passed from one loop to the next?

This is the only way I can imagine that you thought using the same pattern for every file might be ok?

Please or to participate in this conversation.