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.
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
Here is my controller
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
move_uploaded_file($tempname, 'picture/banner/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
here is my route
Route::post('/BannerEdit', [App\Http\Controllers\BannerController::class,
'createAdmin']);
here is my blade form
<form action="{{ url('') }}/BannerEdit" method="post" class="col-12"
enctype="multipart/form-data">
#csrf
<div class="mb-3 col-12">
<label class="col-4 text-label form-label">Banner Photo*</label>
<input type="file" name="bannerPic" class="form-control input-rounded col-4 mb-3"
required>
</div>
<div class="mb-3 text-end">
<input type="submit" class="btn btn-primary" value="Upload">
</div>
</form>
When i submit the data it Gives me an Error as
move_uploaded_file(picture/banner/favicon.jpg): Failed to open stream: No such file or directory
But this Directory Exists
And i checked with full pathof localhost then it does not supports http path
You need to use getcwd() function like this:
$dirpath = realpath(dirname(getcwd()));
So your controller code will be:
public function createAdmin()
{
$photo=$_FILES['bannerPic']['name'];
$tempname=$_FILES['bannerPic']['tmp_name'];
// echo $tempname." ".$photo;
$dirpath = realpath(dirname(getcwd()));
move_uploaded_file($tempname, $dirpath.'/'.$photo);
$data=[
'sliderPic' => $photo,
];
dd($data);
// \App\Models\Banner::create($data);
// return view('Banner');
}
let me know if it works.. if not then we'll modify $dirpath variable
PS: this solution will work on server. are you working on server or in localhost?
EDIT:
Other solution is to use below function to get proper directory structure:
$dirpath = public_path('picture/banner/');
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());
here the user transfers the photo from the device to the form
it is necessary that when registering a user he entered a photo and this photo could be viewed by the administrator in the admin area
web.php
Route::post('/head', 'ImageController#upload')->name('image.upload');
ImageController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ImageController extends Controller
{
public function upload(Request $request)
{
$path = $request - file('image')->store('upload', 'public');
return view('default',['path'=> $path]);
}
}
(php artisan storage:link) entered
input form
<div class="form-group row">
<label for="ydostak" class="col-md-4 col-form-label text-md-right">{{ __('Фото удостоверение с двух сторон') }}</label>
<form action{{ route('image.upload')}}="" method="post" accept-charset="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="file" name="image">
</div>
</form>
</div>
withdrawal form
#isset ($path)
<img class="img-fluid" scr="{{asset('/storage/'. $path)}}" alt="">
#endisset
public function upload(Request $request)
{
$user = new user ;
if($request->hasFile('image')){
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image)->resize(300, 300)->save( storage_path('/uploads/' . $filename ) );
$user->image = $filename;
$user->save();
};
$user->save();
return redirect()->route('user.index')
->with('success','User created successfully');
}
Your form tag is incorrectly formed:
<form action{{ route('image.upload')}}="" method="post" accept-charset="multipart/form-data">
You are not assigning anything to the action as what you have isn't the action attribute. You have something like this:
<form actionhttp://yoursite/head="" ...>
Which isn't the action attribute. You want:
<form action="{{ route('image.upload') }}" ...>
Which will give you something like:
<form action="http://youriste/head" ...>
Once you fix that you can move onto actually dealing with your Controller method.
In add employee form I have to upload profile pic?
is this code is valid??
will anyone help me to modify the code?
This Code Is In My Controller File
$employee = new User();
$file = $request->file('file');
$name = time() . $file->getClientOriginalName();
$file->move('uploads/images', $
$employee->file = $file;
$employee->save();
This IS Code Of My view file
<div class="col-md-6">
<div class="form-group">
<label for="photo">Profile Picture :<span class="danger">*</span> </label>
<input type="file" class="form-control" id="file" name="file">
</div>
</div>
Issue is that everything work properly but in my collection of mongodb file isn't store. it upload in folder, but not in database.
You should change $file variable with $name
$employee->file = $name; // $file may contain file object
$employee->save();