Search code examples
phplaravellaravel-query-builder

Multiple file upload and location store using laravel


I have an app which stores music from user. User can upload multiple files at once. But while adding this code, the database and the controller only uploads the first file, discarding other files. I need to solve this problem. Any solutions?

UploadController.php

 public function store(Request $request)
{
    //This is a complex way to store user id. If any easy way, then help me.
    $curruser = auth()->user();
    $userid = $curruser->id;
    $songs = $request->file('songs');
    $paths = [];

    if(!empty($songs)){
        foreach ($songs as $song){
            $filename = $song->getClientOriginalName();
            $filesize = $song->getSize();
            $extension = $song->getClientOriginalExtension();
            $paths[] = $song->storeAs('songs',$filename);

            $path = new MusicUpload(array(
                'user_id' => $userid,
                'filename' => $filename,
                'extension' => $extension,
                'filesize' => $filesize,
            ));

            $path->save();
            $paths[] = $path;

            return redirect('/fileupload')->with('success','Uploaded successfully');

        }
    }
}

And also I cannot store the location of the file in the mysql server. Any ideas to do that also. Thanks


Solution

  • Use this

    public function store(Request $request)
    {
        $input = $request->all();
        $rules = array(
            'songs.*' => 'required|mimes:jpeg,png,jpg,doc,docx,pdf,mp4,mov,ogg,qt',  //etc
        );
        $validator = Validator::make($input, $rules);
        if ($validator->fails()) {
            $arr = array("status" => 400, "message" => $validator->errors()->first(), "data" => array());
        } else {
            try {
                $datas = [];
                $result = [];
                if ($request->hasfile('songs')) {
                    foreach ($request->file('songs') as $key => $file) {
                       
                        $name = $file->getClientOriginalName();
                        $extension = $file->getClientOriginalExtension();
                        $filesize = $file->getSize();
                        $input['songs'] = time() .uniqid().'.' . $file->getClientOriginalExtension();
                        $file->move(public_path() . '/images', $input['songs']);  // your file path
                        $datas[$key] = $name;
                        $datas[$key] = $extension;
                        $datas[$key] = $filesize;
                
                        $file = new MusicUpload();
                        foreach ($datas as $data) {
                            $file->user_id = Auth::user()->id;
                            $file->filename = $name;
                            $file->extension = $extension;
                            $file->filesize = $filesize;
                            $file->save();
                        }
                    }
                }
                $arr = array("status" => 200, "message" => "success", "data" => $output);
            } catch (\Exception $ex) {
                if (isset($ex->errorInfo[2])) {
                    $msg = $ex->errorInfo[2];
                } else {
                    $msg = $ex->getMessage();
                }
                $arr = array("status" => 400, "message" => $msg, "data" => array());
            }
        }
        return \Response::json($arr);
    }