Skip to content
Advertisement

problem with metadata of multiple audio files using laravel

I’m creating a music player using Laravel. I’m trying to implement ‘laravel-getid3’ package into my project. Here is my code which processes the file and uploads it to the database

public function store(Request $request)
    {
    
        $input = $request->all();
        $datas = [];
        $result = [];
       
        if($request->hasfile('songs')){
            foreach ( $request->file('songs') as $key => $file){
                /*$trackInfo = new getID3($request->file('songs'));
                $tifo = $trackInfo->extractInfo();
                print_r($tifo);
                */
                $nametag = $file->getClientOriginalName();
                $name = explode('.',$nametag)[0];
                $extension = $file->extension();
                $filesize = $file->getSize();
                $input['songs'] = time() .uniqid().'.' . $file->extension();
                $location = $input['songs'];
                $file->storeAs('public/songs',$input['songs']);
                $datas[$key] = $name;
                $datas[$key] = $extension;
                $datas[$key] = $filesize;
                $datas[$key] = $input['songs'];
                
                $file = new MusicUpload();              
                
                foreach ($datas as $data){
                    $file->user_id = Auth::user()->id;
                    $file->filename = $name;
                    $file->extension = $extension;
                    $file->filesize = $filesize;
                    $file->location = $location;
                    //$file->save();
                }
            }
        }

        return response() -> json([
            'uploaded' =>true
        ]);
    }

This code can handle multiple files but is very limited i.e. can only gather a limited amount of info from the files. Trying to implement the package i.e. if I comment the other parts and uncomment the commented part and try to run the code leads to error as it can only handle a single file.

error when uploading multiple files

and I’m unable to loop the code so it can handle each file one by one. Any solutions for the particular code or any new solutions that combats both?

Advertisement

Answer

I see few issues in your flow

  1. you are passing all the songs files array to the getID3, in each iteration

    $trackInfo = new getID3($request->file('songs'));

    this is wrong, by their docs you should pass only one file, the one you are iterating

    i.e $trackInfo = new getID3($file);

  2. your are overriding the $input['songs'] in every iteration, and you don’t need it all, you can just save the location of each file in $location

  3. You have the $datas array, and in every request file you iterating this $datas and it will cause duplication in DB (assuming all will work without any errors).

  4. you iterating the requests songs as $file and then you set it again and overriding it as $file = new MusicUpload(); and this is wrong.

please try this updates code:

public function store(Request $request)
{
    if($request->hasfile('songs')){
        foreach ( $request->file('songs') as $key => $file){
            $trackInfo = new getID3($file);
            $tifo = $trackInfo->extractInfo();
            //dd($tifo);
            
            $nametag = $file->getClientOriginalName();
            $name = explode('.',$nametag)[0];
            $extension = $file->extension();
            $filesize = $file->getSize();
            $location = time() .uniqid().'.' . $file->extension();
            $file->storeAs('public/songs', $location);
            
            $music_upload_file = new MusicUpload();
            $music_upload_file->user_id = Auth::user()->id;
            $music_upload_file->filename = $name;
            $music_upload_file->extension = $extension;
            $music_upload_file->filesize = $filesize;
            $music_upload_file->location = $location;
            $music_upload_file->save();
        }
    }
    
    return response() -> json([
        'uploaded' =>true
    ]);
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement