简体   繁体   English

多个文件上传和位置存储使用 laravel

[英]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.但是在添加这段代码时,数据库和 controller 只上传第一个文件,丢弃其他文件。 I need to solve this problem.我需要解决这个问题。 Any solutions?任何解决方案?

UploadController.php上传控制器.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.而且我也无法将文件的位置存储在 mysql 服务器中。 Any ideas to do that also.也有任何想法可以做到这一点。 Thanks谢谢

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);
}

Just return the redirection after foreach loop has completed.只需在 foreach 循环完成后返回重定向。

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');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM