Reputation: 1886
I created a Forum which should upload an image.
In my form i`ve
{{ Form::file('image') }}
This is a part of my controller:
public function store()
{
$input = Input::all();
$v = Validator::make($input, Post::$rules);
if ($v->passes()) {
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->image = Input::file('image'); // your file upload input field in the form should be named 'file'
$destinationPath = 'uploads/'.str_random(8);
$filename = $post->image->getClientOriginalName();
$extension =$post->image->getClientOriginalExtension(); //if you need extension of the file
$uploadSuccess = Input::file('image')->move($destinationPath, $filename);
$post->m_keyw = Input::get('m_keyw');
$post->m_desc = Input::get('m_desc');
$post->slug = Str::slug(Input::get('title'));
$post->user_id = Auth::user()->id;
$post->save();
return Redirect::route('posts.index');
}
return Redirect::back()->withErrors($v);
}
But laravel stores the image as a .tmp file in my database.
The path in my database is then "/uploads/xxxxx.tmp"
Why does laravel stores the image as .tmp and not as .img ?
What do i wrong and why does laravel stores the image as a .tmp file ?
Upvotes: 3
Views: 8217
Reputation: 181
I solved this problem by deleting the 'image' column in my $fillable variable. In Model Post.php
protected $table = "posts";
protected $fillable = [
'title',
'image', //delete if exists
'content'
];
Upvotes: 1
Reputation: 1
.tmp
file is the file which you select from your local computer. So you need to assign correct path to your model to store it with correct URL.
Upvotes: -1
Reputation: 3636
The problem is in this line
$post->image = Input::file('image');
You assign the .temp image file to your model instance and that's what is stored in the database.
You can do it this way.
$post = new Post;
$post->title = Input::get('title');
$post->body = Input::get('body');
$file = Input::file('image');
$filename = $file->getClientOriginalName();
$destinationPath = 'uploads/'.str_random(8);
// This will store only the filename. Update with full path if you like
$post->image = $filename;
$uploadSuccess = $file->move($destinationPath, $filename);
Upvotes: 2