0

应该有一个简单的解决方案,但我找不到它。因此,在文件上传到 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。

4

1 回答 1

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');
        }   
    }
于 2017-06-02T22:03:43.157 回答