繁体   English   中英

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

[英]Multiple file upload and location store using laravel

我有一个存储用户音乐的应用程序。 用户可以一次上传多个文件。 但是在添加这段代码时,数据库和 controller 只上传第一个文件,丢弃其他文件。 我需要解决这个问题。 任何解决方案?

上传控制器.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');

        }
    }
}

而且我也无法将文件的位置存储在 mysql 服务器中。 也有任何想法可以做到这一点。 谢谢

用这个

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

只需在 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