Reputation: 369
I'm trying to make an image uploader, but it always give me this error
Call to a member function getClientOriginalName() on a non-object
here is my code controller code
public function uploadImageProcess(){
$destinatonPath = '';
$filename = '';
$file = Input::file('image');
$destinationPath = public_path().'/assets/images/';
$filename = str_random(6).'_'.$file->getClientOriginalName();
$uploadSuccess = $file->move($destinationPath, $filename);
if(Input::hasFile('image')){
$images = new Images;
$images->title = Input::get('title');
$images->path = '/assets/images/' . $filename;
$image->user_id = Auth::user()->id;
Session::flash('success_insert','<strong>Upload success</strong>');
return Redirect::to('user/dashboard');
}
}
and here is the upload form
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post">
<label>Judul Poster</label>
<input class="form-control" type="text" name="title">
<label>Poster</label>
<input class="" type="file" name="image"><br/>
<input class="btn btn-primary" type="submit" >
</form>
what's wrong with my code?
Upvotes: 9
Views: 60267
Reputation: 838
if you are using Laravel Collective than you can try this solution
{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
{{ Form::close() }}
else if you are using html form tag than you have to put extra markdown for storing image data
<form class="form form-horizontal" method="post" action="{{ route('user/poster/upload_process') }}" enctype="multipart/form-data">
Upvotes: 0
Reputation: 1
{!! Form::open(array('url' => '/xyz','files' => true)) !!}
{!! Form::close() !!}
Upvotes: 0
Reputation: 307
This is just because you forget to write enctype="multipart/form-data"
in <form>
tag.
This error happen just when you forget this:
<form class="form form-horizontal" method="post" action="{{ route('articles.store') }}" enctype="multipart/form-data">
Upvotes: 1
Reputation: 1
Please Check Your Form 'files'=> true {!! Form::open(['route' => ['Please Type Url'], 'class' => 'form-horizontal' , 'files' => true]) !!}
Upvotes: 0
Reputation: 11
These code are right, but you didn't check values of returns of Input::file('image')
. I think returns value may be is not a correct object or your class Input does not have a public function name is getClientOriginalName
.
Code:
$file = Input::file('image');
var_dump($file); // if return a correct object. you will check your class Input.
Good luck.
Upvotes: 1
Reputation: 22872
You miss enctype
attribute in your form markup.
Either do this
<form role="form" action="{{URL::to('user/poster/upload_process')}}" method="post" enctype="multipart/form-data">
...
</form>
or this...
{{ Form::open(array('url' => 'user/poster/upload_process', 'files' => true, 'method' => 'post')) }}
// ...
{{ Form::close() }}
Upvotes: 33