将上传的文件URL保存到数据库laravel 5.4

时间:2017-06-02 20:53:30

标签: sql laravel-5.4 laravel-filesystem

应该有一个简单的解决方案,但我找不到它。因此,在将文件上传到localhost / localdisk之后(我这样做是出于学习目的)我想将其url(可以将其作为下载链接放入锚标记)保存到数据库。

控制器中的上传功能如下所示:



public function store(Request $request){
		if ($request->hasFile('file')) {
			$filename = $request->file->getClientOriginalName();
			$fileurl = $request->file->path();
			$request->file->storeAs('public/uploads', $filename);
			$file = new File();
			$file->name = $request->name;
			$file->description = $request->description;
			$file->visible = $request->visible;
			$file->file_name = $filename;
			$file->file_url = $fileurl;
			$file->save();		
		}else{
			return view ('upload.files');
		} 	
	}




函数工作正常,但$ fileurl的结果不是我想要的。 在数据库中我看到:C:\ xampp \ tmp \ phpE5FD.tmp,这显然不是一个合适的URL。那么我如何获得存储的正确URL。

1 个答案:

答案 0 :(得分:0)

试试这个

public function store(Request $request){

    $this->validate($request, [
        'file' => 'required',
    ]);
            $file = new File();
        if ($request->hasFile('file')) {
           $file = Input::file('file');
            //getting timestamp
            $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
            $file_name = $timestamp. '-' .$file->getClientOriginalName();
            $file->move(public_path().'/public/uploaded/', $name);
            }
            $file->name = $request->name;
            $file->description = $request->description;
            $file->visible = $request->visible;
            $file->file_name = $file_name;
            $file->file_url = $file_name;
            $file->save();
            return view ('upload.files')->with('status', 'File uploaded Seccess');
        }   
    }