This happens to me when the file is exceeding upload_max_filesize or post_max_size. Hope this helps.
Apr 17, 2017
7
Level 1
Getting error when uploading a file, but it's uploaded to directory
Hi, I try to upload a picture. I can't see what I am doing wrong, since the picture gets uploaded to the public_dir('img'), the public/img directory, but I get an error that says The image failed to upload. and the post is of course not created in my database. In my create.blade I have
{!! Form::model($post, [
'method'=> 'POST',
'route' => 'blog.store',
'files' => TRUE
]) !!}
{!! Form::file('image') !!}
and in the controller I have
protected $uploadPath;
public function __construct()
{
parent::__construct();
$this->uploadPath = public_path('img');
}
.
.
.
public function store(Request $request)
{
$data = $this->handleRequest($request);
$this->validate($request, [
'title' => 'required',
'body' => 'required',
'published_at' => 'date_format:Y-m-d H:i:s',
'category_id' => 'required',
'image' => 'mimes:jpg,jpeg,bmp,png',
]);
$request->user()->posts()->create($data);
return redirect('admin')->with('message', 'Your post was created');
}
public function handleRequest($request)
{
$data = $request->all();
if ($request->hasFile('image'))
{
$image = $request->file('image');
$fileName = $image->getClientOriginalName();
$destination = $this->uploadPath;
$image->move($destination, $fileName);
$data['image'] = $fileName;
}
return $data;
}
Please or to participate in this conversation.