2017-10-05 76 views
-1

上傳腳本正在工作,文件也被正確/所需的名稱保存。然而,儘管在數據庫中存儲數據,它存儲的.tmp文件名,而不是Laravel在數據庫中存儲tmp文件名

控制器代碼:

public function store(Request $request) 
{ 
    $this->validate(request(), [ 
     'title' => 'required', 
     'body' => 'required', 
     'featured_image' =>'image|max:1999' 
    ]); 

    $post = new Post; 

    if ($request->hasFile('featured_image')) { 
     $image = $request->file('featured_image'); 
     // dd($image); 

     $filename = time(). '.' .$image->getClientOriginalExtension(); 
     // dd($filename); 
     $location = public_path('images/' . $filename); 
     // dd($location); 
     Image::make($image)->resize(800, 400)->save($location); 
     // dd($image); 

     $post->image = $filename; 

     // dd($post); 
    } 

    auth()->user()->publish(
     new Post(request(['title', 'body', 'featured_image'])) 
    ); 

    session()->flash('message', 'your post has now been published'); 
    return redirect('/'); 
} 

它存儲的文件名C:\xampp\tmp\phpD837.tmp。怎麼了?

+0

你確定它存儲/更新實際'.tmp'文件名?因爲我在這裏看到的是:你不用'$ post-> save()'保存' – cbaconnier

+1

不要懶得複製代碼而不是添加圖片! – teeyo

回答

1

你與正確的圖像文件名創建一個新的Post

$post = new Post; 
.... 
$post->image = $filename; 

但是當您保存到數據庫中,你不使用$post數據都:

auth()->user()->publish(
    new Post(request(['title', 'body', 'featured_image'])) 
); 

所以基本上您使用POST數據創建另一個新的Post,其中包含臨時PHP文件名,忽略具有所需文件名的第一個Post

試着這麼做:

$post = new Post; 
.... 
$post->image = $filename; 
$post->title = $request->title; 
$post->body = $request->body; 

auth()->user()->publish($post); 
相關問題