failed when upload multiple image with description in laravel - laravel

i am trying to upload image from mobile, with name of the image of it.
i am using flutter for the mobile and using dio to upload it.
but i got error when it upload to laravel server.
here is my code in laravel 9 :
foreach ($request->team as $item) {
$foto = $item->file('id_card');
$path = public_path() . '/upload/idcard/' . Str::slug($workingPermit->id) . Auth::id() . '/images';
$fileName = Str::random(6) . $foto->getClientOriginalName();
$foto->move($path, $fileName);
$workingPermit->tenant_team()->create([
'name' => $item['name'],
// 'id_card' => $item['id_card']
'id_card' => '/upload/idcard/' . Str::slug($workingPermit->id) . Auth::id() . '/images/'. $fileName
]);
}
please help, i really need your help

Related

Google drive error while uploading the file from laravel

throw this error
Google_Exception
(list) unknown parameter: 'maxResults'
// code
$eod = new Eod;
$eod->user_id = auth()->user()->id;
$eod->save();
$folderName = time();
$donationInfo = Society::where('eod_id', $eod->id)->first();
$pdf = \PDF::loadView('pdf.society', compact('donationInfo'));
$fileName = 'society_army-' . $eod->id . '.pdf';
\Storage::disk('public')->put('downloads/donation_' . $folderName . '/' . $fileName, $pdf->output());
$fileAddress = storage_path().'downloads/donation_' . 'downloads/donation_' . $folderName . '/' . $fileName;
$file = new UploadedFile($fileAddress, 'file');
$request->files->set("file['.$j.']", $file);
$drive_result = $this->uploadToGoogleDrive($request, \App\Client::find($request->client_id)->getFullNameAttribute(), $fileName, 'DonationReceipt');
my running website throwing this error while uploading the file on google drive. My logic is to create a pdf and uploaded it to google drive everything running fine for years but now I am seeing this error. Is anyone has any idea about this

Laravel 5.0 files upload validation

I'm trying to upload multiple files via postman. And would like to validate the files before uploading them. I'm using Laravel 5.0 here is my code to upload the files so far:
if ($request->hasFile('attaches')) {
$files = $request->file('attaches');
foreach ($files as $key => $file) {
$extension = $file->getClientOriginalExtension();
$name = time() .$key. '.' . $extension;
$path = 'files' . '/';
$file = $uploadedFile->move($path, $name);
}
}
This code uploads the files just fine but i would like to validate them before the upload as well. How can I do that in Laravel 5.0?
You can use laravel validater to validate your image. You can do this as
$request->validate([
'image' => 'file|required|mimes:png,jpg,jpeg'
]);

i keep getting "function name must be a string" error

I am building a site using laravel, and I got this error when trying to upload an image. I'm a beginner so, can I get some help?
$user = Auth::user();
$user->name = $request('name');
$user->update();
$file = $request->file('image');
$filename = $request->name . '-' . $user->id . '.jpg';
$user->name = $request('name');
$request is variable not func because of $
Because you didn't follow the naming rules in PHP : $request('name') .
I guess you were going to use $request->get('name')
For Uploading image you can use following code, Although I prefer using Intervention Image
//Auth::loginUsingId(1); #You can test your code without a real login
$data = $request->only(['name']);
if (request()->hasFile('image')) {
$file = request()->file('image');
$fileName = md5($file->getClientOriginalName() . time()) . "." . $file->getClientOriginalExtension();
$destinationPath = public_path('/images/'); //// I assume you have a folder named images in public.
$file->move($destinationPath, $fileName);
$data['avatar'] = $destinationPath.$fileName; // I assume you have a field named avatar in users table.
}
return $data; // Try this line for a better understanding then comment it
Auth::user()->update($data);

Laravel deploy: storage image doesn't work correctly

After deploying my laravel project from local to Apache web server, all works correctly except images link. Here the code:
Images are stored in:
storage/app/public/photos
after i've run command:
php artisan storage:link
Images are linked at:
public/storage/photos
Controller:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photo = $file->storeAs('public/photos', 'foto-' . time() . '.' . $extension);
$user->photo = $photo;
$user->save();
}
Images are uploaded correctly on storage/app/public/photos and correctly linked in public/storage/photos but it doesn't display on frontend.
in blade, i've tried to use Storage::url to retrieve the path
{{Storage::url($user->photo)}}
and asset()
{{asset($user->photo)}}
in both cases, image doesn't exist
The public path of image is:
http://mywebsite.com/storage/photos/foto-1522914164.png
You should Use url function to show your image like below way.
url($user->photo);
I'd suggest to change the controller code as follows:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'foto-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$user->photo = 'photos/' . $photoFileName;
$user->save();
}
Then you can use {{asset($user->photo)}} in your blade.
on my webspace, it seems that the only way to display image correctly is to create a custom route that read and serve the image.
i'm solved like this:
i'm storing only image name in db:
if ($request->hasFile('photo')) {
$extension = $request->file('photo')->getClientOriginalExtension();
$file = $request->file('photo');
$photoFileName = 'photo-' . $model->id . '.-' . time() . '.' . $extension;
$photo = $file->storeAs('public/photos', $photoFileName);
$store = $photoFileName;
}
then, i've create custom route that read images and display them:
Route::get('storage/{filename}.{ext}', function ($filename, $ext) {
$folders = glob(storage_path('app/public/*'), GLOB_ONLYDIR);
$path = '';
foreach ($folders as $folder) {
$path = $folder . '/' . $filename . '.' . $ext;
if (File::exists($path)) {
break;
}
}
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;
});
in blade, i'm using Storage to display image:
{{ Storage::url($photo->photo) }}}

Image resize error using intervention in laravel 5

I am using intervention image to resize image and then save it to a destination folder. But its not working for me. I am getting error like, "Image source not readable".
Pls see the below code:
$image_name = $file->getClientOriginalName();
$thumbName = 'thumb_'. $image_name;
$destinationPath = public_path() . '/uploads/';
$thumbdestinationPath = public_path() . '/uploads/thumbnails/';
$imgUrl = URL::to('/').'/public/uploads/'.$image_name;
$thumbUrl = URL::to('/').'/public/uploads/thumbnails/'.$image_name;
$upload_success = $file->move( $destinationPath, $image_name);
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath );
}
You need to give the name of image:
if ($upload_success) {
Image::make($file->getRealPath())->fit('120','120')->save($thumbdestinationPath . $image_name );
}

Resources