updated image stores in database but not folder in laravel - laravel-5

problem is that company_photo stores in database but not store in company_photos folder, please tell me where i going wrong..
public function update(Request $request, Company $company,CompanyOtherInfo $CompanyOtherInfo )
{
//dd($company);
//dd(request()->all());
if($request->hasFile('company_photo')){
$files=public_path().'/company_photos/'.$req->input('company_photo1');
File::delete($files);
//dd($files);
$file = $req->file('company_photo');
$destinationPath = public_path().'/company_photos/';
$filename = $file->getClientOriginalName();
$file->move($destinationPath, $filename);
// echo $filename;
}
else{
$filename=$request->input('company_photo1');
}
//dd($request);
//dd($filename);
$company::where('companies.company_id',$company->company_id)
->update([
'company_photo' => $filename,
'company_name' => request('company_name'),
'company_email' => request('company_email'),
'company_mobile' => request('company_mobile'),
'company_country' => request('company_country'),
'company_state' => request('company_state'),
'company_city' => request('company_city'),
'company_pincode' => request('company_pincode'),
'company_address' => request('company_address'),
'industry_id' => request('industry_id'),
'segment_id' => request('segment_id'),
'company_code' => request('company_code'),
'contact_person'=>request('contact_person'),
]);

The issue you had is you changed the variable $request to $req half way through your code:
Change these lines:
$files = public_path().'/company_photos/'.$req->input('company_photo1');
$file = $req->file('company_photo');
to:
$files = public_path().'/company_photos/'.$request->input('company_photo1');
$file = $request->file('company_photo');
Here is the code with a little refactor:
$filename = $request->input('company_photo1');
if ($request->hasFile('company_photo')) {
$files = public_path().'/company_photos/'.$fileName;
File::delete($files);
$file = $request->file('company_photo');
$file->move(public_path().'/company_photos/', $file->getClientOriginalName());
}

Related

image not store in database laravel

I want to Create app in laravel that manage my events
i use this code for EventController
public function store(Request $request)
{
$request->validate([
'inviter'=>'max:255',
'date'=>'max:255',
'phone'=>'max:255',
'whatsapp'=>'max:255',
'location'=>'max:255',
'approval'=>'max:255',
'number'=>'max:255',
'description'=>'max:255',
'track'=>'max:255',
]);
$eve = new Event([
'inviter'=> $request->get('inviter'),
'date'=> $request->get('date'),
'phone'=> $request->get('phone'),
'whatsapp'=> $request->get('whatsapp'),
'location'=> $request->get('location'),
'number'=> $request->get('number'),
'approval'=> $request->get('approval'),
'description'=> $request->get('description'),
'track'=> $request->get('track'),
]);
if ($request->hasFile('file')) {
$file = $request->file('file');
$fileName = $file->getClientOriginalName();
$filePath = time() . '.' . $file->getClientOriginalName();
$request->file->move(public_path('uploads/events'), $filePath);
$eventImage = Image::createNew();
$eventImage->filename_path = $filePath;
$eventImage->original_filename = $fileName;
$eventImage->event_id = $eve->id;
$eventImage->save();
}
return redirect(route('event.index'))->with('success','Event Created');
}
but image dont create I think related to event_id when I was testing the code correctly and incorrectly
Try this
$eve = Event::create([
'inviter'=> $request->get('inviter'),
'date'=> $request->get('date'),
'phone'=> $request->get('phone'),
'whatsapp'=> $request->get('whatsapp'),
'location'=> $request->get('location'),
'number'=> $request->get('number'),
'approval'=> $request->get('approval'),
'description'=> $request->get('description'),
'track'=> $request->get('track'),
]);

Sending an image with HTTP POST

Recently I wanted to separate my project in different services to I wanted to make blogs independent from the project.
In the first project i have written this code. I want to send the data that i get from the form to another API http://127.0.0.1:100/api/saveBlog
public function update(Request $request, $blog)
{
if (!$blog instanceof Blog) {
$blog = $this->getById($blog);
}
$response = Http::post("http://127.0.0.1:100/api/saveBlog",[
'name' => $request->input('name'),
'description' => $request->input('description'),
'name' => $request->input('name'),
'photto' => $request->file('photto')
]);
dd($response->status());
}
In the API service i am trying to read the data
Route::post("/saveBlog",function (Request $request){
$blog = new Blog();
$blog->name = $request->input('name');
$blog->description = $request->input('description');
$blog->name = $request->input('name');
$main = $request->file('photto');
$fileName = microtime() . '.' . $main->getClientOriginalExtension();
$img = Image::make($main->getRealPath());
$img->resize(400, 400);
$img->stream();
Storage::disk('local')->put('public/blogs/' . $fileName, $img, 'public');
$blog->image_path = "/storage/blogs/" . $fileName;
return $blog->save();
});
But i am getting 500 status error and blog is not being saved in database.
I think the problem is with $request->file('photto')
ANY IDEA?
check whether image exist in request like below
if($request->has('photto')){
$main = $request->file('photto');
$fileName = microtime() . '.' . $main->getClientOriginalExtension();
$img = Image::make($main->getRealPath());
$img->resize(400, 400);
$img->stream();
Storage::disk('local')->put('public/blogs/' . $fileName, $img, 'public');
$blog->image_path = "/storage/blogs/" . $fileName;
}
Updates
$photo = fopen(public_path('/storage/filename'), 'r');
$response = Http::
attach('photo', $photo)
->post($url, [
'param_1' => 'param_1 contents',
...
]);

Change directory for uploading image

How can I change the directory of the uploaded images.
I want to upload it in the App/public/files folder.
My code here:
public function update(Request $request, Organigramme $organigramme)
{
$id = $organigramme->id;
$organigramme = Organigramme::find($id);
$organigramme->matricule = $request->input('matricule');
if ($request->has('profile_image'))
{
$image = $request->file('profile_image');
$name = str_slug($request->input('matricule'));
$folder = '/uploads/images/';
$filePath = $folder . $name. '.' . $image->getClientOriginalExtension();
$this->uploadOne($image, $folder, 'public', $name);
$organigramme->profile_image = $filePath;
$organigramme->save();
}
return view( 'admin.organigrammes.show', compact('organigramme'));
}
public function uploadOne(UploadedFile $uploadedFile, $folder = null, $disk = 'public', $filename = null)
{
$name = !is_null($filename) ? $filename : str_random(25);
$file = $uploadedFile->storeAs($folder, $name.'.'.$uploadedFile->getClientOriginalExtension(), $disk);
return $file;
}
In the config/filesystems file add a new save path.For example:
'images' => [
'driver' => 'local',
'root' => base_path().'/app/public/files',
],
In the code itself, use the following code to save:
Storage::disk('images')->put($path, $image);

Upload multiple images from different input type files in laravel

I have 4 input type file with different name.How can i upload 4 images into the public folder and into 4 different column in database in same time?
$files=[];
if($request->hasfile('image_front_before')) $files[] = $request->file('image_front_before');
if($request->hasfile('image_back_before'))$files[] = $request->file('image_back_before');
if($request->hasfile('image_left_before'))$files[] = $request->file('image_left_before');
if($request->hasfile('image_right_before'))$files[] = $request->file('image_right_before');
if i dd(files); it will show below..It will appear the images that i have upload
array:4 [▼
0 => UploadedFile {#441 ▶}
1 => UploadedFile {#445 ▶}
2 => UploadedFile {#439 ▶}
3 => UploadedFile {#442 ▶}
]
I think the problem is here..
foreach ($files as $file)
{
if(!empty($file))
{
$filename= md5(time()).'.'.$file->getClientOriginalExtension();
$path = app(GlobalBookingController::class)- >getBookingImageDirectory();
$location = public_path($path.$filename);
Image::make($file)->save($location);
$data[]=$filename;
}
}
Below is the code to insert images into database
$vehicle_image =BookingVehicleImage::updateOrCreate(
[
'booking_id' => $id
],
[
'image_front_before' =>$data[0],
'image_back_before' =>$data[1],
'image_left_before' =>$data[2],
'image_right_before' =>$data[3]
]
);
if i upload 4 images in the same time only 2 will be upload into into public folder ..Sometimes for column image_left_before and image_right_before the images is correct..but for the column image_front_before will contain image_left_before and image_back_before will contain image_right_before..Sometimes only one column has right images and the other column has 3 same images even upload the different images at the start.
Try this example
public function store(Request $request)
{
$this->validate($request, [
'image1' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image2' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image3' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'image4' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$article = new Article();
if ($request->hasFile('image1')) {
$image = $request->file('image1');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image1 = $name;
}
if ($request->hasFile('image2')) {
$image = $request->file('image1');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image2 = $name;
}
if ($request->hasFile('image3')) {
$image = $request->file('image3');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image3 = $name;
}
if ($request->hasFile('image4')) {
$image = $request->file('image');
$name = str_slug($request->title).'.'.$image->getClientOriginalExtension();
$destinationPath = public_path('/uploads/articles');
$imagePath = $destinationPath. "/". $name;
$image->move($destinationPath, $name);
$article->image4 = $name;
}
$article->title = $request->get('title');
$article->category_id = $request->get('category_id');
// $article->image = str_slug($request->get('image'));
$article->subtitle = $request->get('subtitle');
$article->description = $request->get('description');
$article->save();
return back()->with('success', 'Your article has been added successfully. Please wait for the admin to approve.');
}
This code is for saving multiple images to the particular path and to database
$i = 0;
foreach ($files as $file)
{
if(!empty($file))
{
$fileName = time().$i++.'.'.$file->getClientOriginalExtension();
Image::make($file)->save(public_path('/img/'.$fileName)); //This line will save your image to the particular path
array_push($files,$fileName);
$obj->image = $fileName;
$obj->save(); //save data to database
}
}

Save multiple upload with clientOriginalName

hy everyone,, i want to upload multiple file with OriginalClientName, save into database with column called "document" but when data saved into database the file when uploading is not same the name,, i am upload file with name "cv bimo.docx", but in database, the name like this:
C:\Users\bimo_an\AppData\Local\Temp\phpAAF.tmp
i already using method getClientOriginalName(),,
this is my Function controller code :
..............................
$uploadFile = $request->file('document');
foreach($uploadFile as $file){
$filename = $file->getClientOriginalName();
$folder[] = $file->storeAs('uploads', $filename);
}
$data = [
'mto_number'=>$request->txtDocNumber,
'item_code'=>$request->txtItemCode[$key],
'required_qty'=>$request->txtRequiredQty[$key],
'spare_qty'=>$request->txtSpareQty[$key],
// 'file' => $path[$key]
'category' => $request->category[$key],
'document' => $file
];
ModelMTOItem::insert($data);
You are passing file Path instead of clientOriginalName
$uploadFile = $request->file('document');
foreach($uploadFile as $file) {
$filename = $file->getClientOriginalName();
$data = [
'mto_number'=>$request->txtDocNumber,
'item_code'=>$request->txtItemCode[$key],
'required_qty'=>$request->txtRequiredQty[$key],
'spare_qty'=>$request->txtSpareQty[$key],
// 'file' => $path[$key]
'category' => $request->category[$key],
'document' => $filename
];
ModelMTOItem::insert($data);
$folder[] = $file->storeAs('uploads', $filename);
}

Resources