I'm currently learning laravel and I'm stuck at uploading image gallery & header image to a specific folder. This is my controller code: (I'm uploading images on "edit" page)
public function update(Request $request, $id)
{
$findObject = Accommodation::find($id);
$findObject->update($request->all());
Gallery::destroy('objects_id', $id);
foreach ($request['img'] as $img) {
$gallery = new Gallery();
$gallery->objects_id=$id;
$gallery->img=$img;
$gallery->save();
$file[0] = $request->file;
$name = time() . $file[0]->getClientOriginalName(); // prepend the time (integer) to the original file name
$file[0]->move('uploads', $name); // move it to the 'uploads' directory (public/uploads)
// // create instance of Intervention Image
$img = Image::make('uploads/'.$name)->resize(300,200);
$img->save(public_path().'/uploads/'.$name);
}
And this is my view for uploading gallery & header image:
<div class="form-group">
<label for="exampleInputFile">Index image:</label>
<input type="file" name="headerImage" value="{{$object->headerImage}}">
<img src="{{asset('FrontAssets/img/smjestaj/')}}/{{$object->headerImage}}" style="width: 100px;">
<p class="help-block">Image that will be displayed on index page.</p>
<div class="form-group">
<label for="exampleInputFile">Image gallery</label>
<input type="file" name="img[]" multiple>
<p class="help-block">Choose x images for that will be displayed on gallery page.</p>
And it displays me this error:
Call to a member function getClientOriginalName() on null
Note: I have "enctype="multipart/form-data" in my form.
This is what I get when i do dd();
I hope I described my problem correctly, if I need to add anything else please let me know. Thanks in advance!
As per your html
<input type="file" name="headerImage" value="{{$object->headerImage}}">
and
<input type="file" name="img[]" multiple>
Try changing
From
foreach ($request['img'] as $img) {
$gallery = new Gallery();
$gallery->objects_id=$id;
$gallery->img=$img;
$gallery->save();
$file[0] = $request->file;
$name = time() . $file[0]->getClientOriginalName(); // prepend the time (integer) to the original file name
$file[0]->move('uploads', $name); // move it to the 'uploads' directory (public/uploads)
// // create instance of Intervention Image
$img = Image::make('uploads/'.$name)->resize(300,200);
$img->save(public_path().'/uploads/'.$name);
}
To
if($request->file('img'))
{
foreach ($request->file('img') as $key => $file) {
$gallery = new Gallery();
$gallery->objects_id=$id;
$gallery->img=$file;
$gallery->save();
$name = time() . $file->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$file->move($destination, $name);
}
}
if($request->file('headerImage'))
{
$name = time() . $request->file('headerImage')->getClientOriginalName();
$destination = base_path() . '/public/uploads';
$request->file('headerImage')->move($destination, $name);
}
Related
I am working with PHP Laravel 8. I am doing avatar upload to the database and uploading it to public/assets. It's working with database but it doesn't upload it I want some help Thank You
This is HTML code for uploading
<div class="form-group">
<label for="exampleInputFile">Profile Image</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="profile_image" class="custom-file-input" id="profile-image">
<label class="custom-file-label" for="exampleInputFile">Recommended Profile Image (160w x 160h)</label>
</div>
</div>
<span class="font-italic text-warning">Picture size should be less than 4MB</span>
</div>
and here is the controller code
public function update(Request $request)
{
$user = Auth::user();
// $user->full_name=$request->input('full_name');
//$user->email=$request->input('email');
$user->avatar=$request->input('profile_image');
if($request->hasFile('profile_image'))
{
$avatar = $request->file('profile_image');
$filename = time().'.'.$avatar->getClientOriginalExtension();
$location = url('public/assets/uploads/images/'.$filename);
Image::make($avatar)->resize(300, 300)->save($location);
$user = Auth::user();
$user->avatar = $filename;
}
auth()->user()->save();
$user->save();
return redirect()->back()->with('success','Sucessfully changed User Information');
}
To store the image, you can simply do :
if ($request->hasFile('profile_image')) {
$avatar = $request->file('profile_image');
$filename = now()->timestamp . '.' . $avatar->getClientOriginalExtension();
$image->move(public_path('assets/uploads/images/'), $filename);
// the rest of your code
}
To modify the image's size and store it, you can follow the
Laravel 8 Image Resize & Upload with Intervention Image article.
Just change
$location = url('public/assets/uploads/images/'.$filename);
with
$location = public_path('assets/uploads/images/'.$filename);
final
public function update(Request $request)
{
$user = Auth::user();
// $user->full_name=$request->input('full_name');
//$user->email=$request->input('email');
$user->avatar=$request->input('profile_image');
if($request->hasFile('profile_image'))
{
$avatar = $request->file('profile_image');
$filename = time().'.'.$avatar->getClientOriginalExtension();
$location = public_path('public/assets/uploads/images/'.$filename);
Image::make($avatar)->resize(300, 300)->save($location);
$user = Auth::user();
$user->avatar = $filename;
}
auth()->user()->save();
$user->save();
return redirect()->back()->with('success','Sucessfully changed User Information');
}
I created a user table with employee's details, and in my image column I have a file uploaded in its cell. How can I keep old file every time I upload a new file.
my view.blade
<input type="file" name="image[]" class="form-control" value="{{ $employee['image'] }}" multiple>
<th> {{$employee['image']}}</th>
my controller
if($request->hasfile('image')){
$files = [];
foreach ($request->image as $image) {
$path = $image->getClientOriginalName();
$filename = time() . '-' . $path;
$files[] = $filename;
$image->storeAs('employees', $employee->id . '/' . $filename);
$image->move(public_path('employees'),$filename);
}
$files = implode("</br>;", $files);
}
$employee->image = $files;
$employee->save();
Just change one line :
$employee->image = $files;
to,
$employee->image = $employee->image . $files;
This function uploads photos:
public function addImageForNewRecp(Request $request)
{
$file = $request->file('images');
$imageName = $file->getClientOriginalName();
$imageName = str_replace(' ', '-', strtolower($imageName));
$imageName = time() . $imageName;
$file->move(public_path() . '/storage/', $imageName);
return response()->json(["key" => $imageName]);
}
I think I need a for loop to loop all the filenames write but I couldn't figure out where to put it, please help
Additional code:
<input type="file" multiple="multiple" id="recpfiles" class="recpfiles" name="images1" accept="image/* , image/tiff" >
For upload multiple files from the same input field, you just have to make a name with array like below
<input type="file" multiple="multiple" id="recpfiles" class="recpfiles" name="images1[]" accept="image/* , image/tiff" >
In the controller use like below
$files = $request->file('images'); // it will return array
For better understanding you can follow this article
You are missing [ ] in your select name attribute
<input type="file" multiple="multiple" id="recpfiles" class="recpfiles" name="images1[]" accept="image/* , image/tiff" >
How can I display uploaded image from stored location and I want to carry over the same image name to
<input id = "Picture" type="file" name="Picture" accept="image/*" />
This code is used in Controller.php when upload
$img = $input['Picture'];
$name = $img->getClientOriginalName();
$uploaded = $img->move('public/img', $name);
This is where uploaded image will show in my new.blade.php
<img src="#" id="Image" name="Image" />
And Image name doesn't save original name to mysql, it saved like C:xampp mpphpB7CF.tmp
How can I do to save original name that I Uploaded.
Check below code this is working code.
$file = $request->file('Picture');
$destinationPath = 'public/img/';
$originalFile = $file->getClientOriginalName();
$file->move($destinationPath, $originalFile);
This line $originalFile = $file->getClientOriginalName(); will assign original file name to $originalFile and use this variable name where you insert image in database.
Add below line to display image you have uploaded.
<img src='{{ asset('img/'.$originalFile) }}'>
$path = '';
if ($request->hasFile('Picture')) {
$file = $request->file('Picture');
$path = $file->storeAs(public_path('img'), $imageName);
//or
if (file_put_contents(public_path(img) . $imageName, $file)) {
$path = public_path(img) . $imageName;
}
}
return view('your-view', ['img_path' => $path]);;
and in your new.blade.php
<img src="{{ $img_path }}" id="Image" name="Image" />
In Controller
public function show($name)
{
$extension = File::extension($name);
$path = public_path('storage/' . $name);
if (!File::exists($path)) {abort(404);}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
}
Route
Route::get('/images/{name}', 'FileController#show');
Index.blade.php
<a href='{{ asset("images/$hire_details->file") }}'>click here to view image</a>
Return path from controller and display :
<img src='{{ asset('img/'.$name) }}'>
Return your $uploaded from controller to view
return view('new',['uploaded'=>$uploaded]);
and then in your view
<img src='{{ asset($uploaded) }}' >
I think you can try this:
use Input;
use Image;
$file = Input::file('Picture');
$imageName = $file->getClientOriginalName();
$imgUpload = Image::make($file)->save(public_path('img/' . $imageName));
<img src="{{ asset('img/'.$imageName) }}">
Note: If you have get error for Image then install package for Image from this link
Hi I'm trying to upload an image with Laravel 5
Function is:
$file_name = '';
//validation of image before uploading and saving
if( Input::hasFile('img') && Input::file('img')->isValid() ){
$file = Input::file('img'); //creating an object
$file_name = str_random(30) . '.' . $file->getClientOriginalExtension(); //randon str name to img file with the ext of the original file
$file->move( public_path() . '\assets\img', $file_name);
}
form: multipart/form-data
input: input type="file" name="img"
The problem is that there's always an empty value in $file_name
Your form opening tag should have enctype="multipart/form-data". Look like this:
<form method="POST" enctype="multipart/form-data">
And to move your image in storage, write your code in controller like this:
public function store(Request $request)
{
if($request->hasFile('img') && $request->file('img')->isValid()){
$file = $request->file('img');
$file_name = str_random(30) . '.' . $file->getClientOriginalExtension();
$file->move(base_path() . '/assets/img', $file_name);
}
}