2017-10-05 18 views
-1

アップロードスクリプトが動作していますが、ファイルも正しい/希望の名前で保存されます。それはC:\xampp\tmp\phpD837.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('/'); 
} 

:データベース内のデータを格納しながらしかし、それは代わりに

コントローラー・コードを.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を無視して、PHPのファイル名を含むPOSTされたデータで別の新しいPostを作成します。

は次のようなものを試してみてください:

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

auth()->user()->publish($post); 
関連する問題