簡體   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