Getting this error after updating the image.
Call to a member function getClientOriginalExtension() on string
This is the before code. If I dd(); this code I get the file name with the extension but it's a string. So I cannot get the original extension.
$image = $request->warrantyUpload;
$destination = public_path('/uploads/');
$ext = $image->getClientOriginalExtension();
$mainFilename = "img_".rand(111111,999999);
$image->move($destination, $mainFilename.".".$ext);
$image->warrantyUpload = $mainFilename.".".$ext;
$data->warrantyUplaod = $image->warrantyUpload;
$data->save();
I changed my code to this. If I dd(); this code I don't get anything. Just a NULL.
$image = $request->file('warrantyUpload');
$destination = public_path('/uploads/');
$ext = $image->getClientOriginalExtension();
$mainFilename = "img_".rand(111111,999999);
$image->move($destination, $mainFilename.".".$ext);
$image->warrantyUpload = $mainFilename.".".$ext;
$data->warrantyUplaod = $image->warrantyUpload;
$data->save();
And the error now is :
Call to a member function getClientOriginalExtension() on null
I have also tried 'PATCH' and 'PUT' both methods.
This is View Code :
<form action="{{ route('assets.update', $data->_id)}}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group col-10">
<label for="warrantyUpload">Warranty Card:</label>
<input type="file" class="form-control p-1" required="required" value="{{$data->warrantyUpload}}" name="warrantyUpload">
</div>
<div class="form-group col-10">
<button type="subumit" class="btn btn-primary">Submit</button>
</div>
</form>
Here is the dd($request->all());
DD($request->all());
Related
I've looked at code from other people and try to implement the same thing, but the application could not store the image to the desire folder, would you please have a look at what the reason is? Thank you!
Here is my code for route:
Route::post('upload_pic', 'UploadController#storePhoto');
Here is my code for the php laravel template:
<div class="panel-body">
<form action="{{url('/upload_pic')}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="upload-user-photo">Upload Photos</label>
<input type="file" name="image" class="form-control" placeholder="Upload Student Image, Size:207(W)x408(H)">
</div>
<div class="row">
<div class="col-sm-4">
<input class="btn btn-success" type="submit" value="Upload Student Photo" name="submit">
</div>
</div>
</form>
</div>
Here is my code in the controller:
public function storePhoto(Request $request){
$valid = $request->validate([
'image' => 'required|image|mimes:jpg,png,jpeg,|dimensions:max_width=272,max_height=408,min_width=271,min_height=407'
]);
$data = new Postimage();
$file = $request->file('image');
$filename = $file->getClientOriginalName();
// dd($filename);
$file -> move(public_path('./public/img/student_photos'),$filename);
$data['image'] = $filename;
$data->save();
return redirect('/upload')->with('success', 'Photo Uploaded');
}
Your problem is somewhere here
$filename = $file->getClientOriginalName();
$file -> move(public_path('./public/img/student_photos'),$filename);
First, the file comprises only the file extension. Instead, you should have something like this $filename = 'name.' . $file->getClientOriginalName(); Notice the dot in 'name.'
Secondly, no need to add public to the file path string. So it should be $file->move(public_path('img/student_photos'),$filename);
Finally, make sure the upload folder exists and is writeable
I am new in larval development, I am unable to upload image in local folder and I am getting error getClientOriginalExtension() on null. Even I have added code enctype="multipart/form-data" in my form, but still am getting the error. Can anyone give suggestion how to resolve this issue. I have given below my code.
MyBlade file
****MyBlade file****
<form action="{{ route('register.post') }}" method="POST" enctype="multipart/form-data" accept-charset="utf-8" >
#csrf
<div class="form-group row">
<label for="uname" class="col-md-4 col-form-label text-md-right">User Name</label>
<div class="col-md-6">
<input type="text" id="username" class="form-control" name="username" required />
#if ($errors->has('username'))
<span class="text-danger">{{ $errors->first('username') }}</span>
#endif
</div>
</div>
<div class="form-group row{{ $errors->has('image') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 col-form-label text-md-right">Profile Picture</label>
<div class="col-md-6">
<input id="image" type="file" class="form-control" name="image">
</div>
</div>
My Controller File
if ($request->input('image')!=null){
$files = $request->input('image');
$extension = $files->getClientOriginalExtension();
$filaname = time().','.$extension;
$files->move('public/image/',$filaname);
// $post->image= $filaname;
}
else{
return $request;
}
I am getting err:Call to a member function getClientOriginalExtension() on bool
If you use $request->file('image') instead of $request->input('image') you have fixed the problem. The image will then be moved to the public/image folder.
However, if you want to upload the file to the storage/public folder, you can use the storeAs() method on the $file to store it in the storage folder.
// Your controller
public function update(Request $request, $postId)
{
if (! $request->hasFile('image')) {
return $request;
}
// Use file() instead of input()
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
// Note you have a typo in your example, you're using a `,` instead of a `.`
$filename = time().'.'.$extension;
// To store (move) the file to the 'public/image' folder, use this:
$file->move('image', $filename);
// To store the file to the 'storage/app/public/image' folder, use this:
$file->storeAs('public/image', $filename);
// Find your post based on some id
$post = Post::find($postId);
// Save the filename to the database on the Post model:
$post->image = $filename;
$post->save();
}
Hope I helped you with this.
When I am uploading an image to the form and returning it from the controller, the image name and extention are changing.I am a beggar, so if I make a mistake while asking a question, I would like to apologize.
This is my form:
<form action="{{ route('admin.slider.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-12">
<label class="control-label">Image</label>
<input type="file" name="image">
</div>
</div
<button type="submit" class="btn btn-success">Save</button>
</form>
This is my controller:
public function store(Request $request)
{
$this->validate($request, [
'image' => 'required|mimes:jpeg,bmp,png,jpg',
]);
$image = $request->file('image');
return $image;
}
My image file name is :demo.jpg
Controller return result is like that:
C:\xampp\tmp\php5E86.tmp
This is the same result when I give another picture, only the last four characters are changing.
C:\xampp\tmp\phpF239.tmp
It is very helpful to know why I am getting .tmp file return.
use getClientOriginalName to get orginal file name
$request->image->getClientOriginalName()
To get file extension
$request->image->extension();
or
$name = $request->file('image')->getClientOriginalName();
$extension = $request->file('image')->extension();
Ref:https://laravel.com/docs/8.x/filesystem#other-uploaded-file-information
I keep on getting this error when I try to upload a pdf document file, does anybody have any idea how to solve this?
I had tried looking for similar problems but still can't solve it(eg: Upload pdf file using Laravel 5)
I tried doing dd() to see if the file have been uploaded and it did show the file name but the error said Call to a member function getClientOriginalName() on null, so now I'm kind of confused on what to do now.
Here are my codes, thanks in advance for helping.
Controller:
class CreateController extends Controller
{
public function create(){
return view('create');
}
public function store(Request $request){
$uniqueFileName = uniqid() . $request->get('upload_file')->getClientOriginalName() . '.' . $request->get('upload_file')->getClientOriginalExtension();
$request->get('upload_file')->move(public_path('files') . $uniqueFileName);
//dd($request);
return redirect()->back()->with('success', 'File uploaded successfully.');
}
create.blade.php
<form enctype="multipart/form-data" class="form-horizontal" method="post" action="{{ url('/user')}}">
{{ csrf_field() }}
<div class="form-group">
<label for="upload_file" class="control-label col-sm-3">Upload File</label>
<div class="col-sm-9">
<input class="form-control" type="file" name="upload_file" id="upload_file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
Route:
Route::get('/user/create','CreateController#create');
Route::post('/user','CreateController#store');
This enctype solved function call on the null problem
<form method="" action="" enctype="multipart/form-data">
You need to use the file() function to have access to your uploaded file
$request->file('upload_file')
use this code in your tag form enctype="multipart/form-data"
<form method="" action="" enctype="multipart/form-data">
Try to put the file into a var.
$file = $request->file('upload_file');
And get the extension and name from it.
$uniqueFileName = uniqid() . $file->getClientOriginalName() . '.' . $file->getClientOriginalExtension();
Hope it helps.
The key to solving this problem is that you must write getClientOriginalName() function in the if clause just like that:
if($request->hasFile('logoImage')){
$logoImage = $request->file('logoImage');
$name = $logoImage->getClientOriginalName();
}
if($request->has('sound_file')) {
$image = $request->file('sound_file');
$filename = $image->getClientOriginalName();
request()->$image->move(public_path('images/users'), $filename);
$sounds->image = $request->file('sound_file')->getClientOriginalName();
}
Using Laravel 5.4, I'm tring to setup a form where a user can enter a food name and its image. While trying to upload a PNG or JPG image, I get the following Validation Error:
The image must be an image.
If I remove the validation for image, I get a FatalErrorException:
Call to a member function getClientOriginalExtension() on null
which is a helper function provided by Intervention/Image library. This could mean that the image didn't upload at all.
FORM
<form action="/hq/foods" method="POST">
{{ csrf_field() }}
<input type="text" name="name">
<input type="file" name="image">
<button type="submit" class="btn btn-success ">
ADD FOOD ITEM
</button>
</form>
CONTROLLER
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|min:2|max:255',
'image' => 'required|image'
]);
$food_category = FoodCategory::find($request->food_category_id);
$food = new Food;
$food->name = $request->name;
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('img/foods/' . $filename);
Image::make($image)->resize(800, 400)->save($location);
$food->image = $filename;
$food->save();
Session::flash('success',
'
<h4>Success!</h4>
<p>The food item has been added to the Menu.</p>
');
return back();
}
What am I missing?
To upload images you need to add:
enctype="multipart/form-data"
to your form so instead of:
<form action="/hq/foods" method="POST">
you should use
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
If you use Laravelcollective
{!! Form::open(['method'=>'post','files' => true,'url'=>'/hq/foods']) !!}
OR
Using HTML
<form action="/hq/foods" method="POST" enctype="multipart/form-data">
you need to add enctype="multipart/form-data" in your from