Google drive error while uploading the file from laravel - 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

Related

Laravel 8 & Media Library: Associating files

I am using Media Library v9. I am trying to follow this documentation to associate files. However, the package is newly installed to the project and there're lots of images uploaded. Is it possible to add the uploaded media to collection? Like the docs:
$yourModel = YourModel::find(1); // Maybe I can use `id` here instead of a number
$yourModel
->addMedia($pathToFile)
->toMediaCollection();
For my case I have two directories: public/app/lostFound/lostItems and public/app/lostFound/handoverStatements
This was my old code for the image upload:
if( $request->hasFile('LFImage') ) {
$destination = public_path('app/lostFound/lostItems' . $lostFound->LFImage);
if( File::exists($destination) )
{
File::delete($destination);
}
$file = $request->file('LFImage');
$extension = $file->getClientOriginalExtension();
$filename = $lostFound->LFNumber . '-' . $lostFound->lostItem . '.' . $extension;
$file->move('app/lostFound/lostItems', $filename);
$lostFound->LFImage = $filename;
}
if( $request->hasFile('handoverStatement') ) {
$destination = public_path('app/lostFound/handoverStatements' . $lostFound->handoverStatement);
if( File::exists($destination) )
{
File::delete($destination);
}
$file = $request->file('handoverStatement');
$extension = $file->getClientOriginalExtension();
$filename = $lostFound->lostItem . '-' . $lostFound->LFNumber . '.' . $extension;
$file->move('app/lostFound/handoverStatements', $filename);
$lostFound->handoverStatement = $filename;
}
How can I associate the already uploaded LFImage and handoverStatement to the media collection?
This is what I did so far, but it's not directing to the files, but the folder containing the files, which will obviously gives me an error:
$lostFound = LostFound::find('id');
$lostFound
->addMedia( public_path('app/lostFound/handoverStatement') )
->toMediaCollection();

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 saves temp file as image

I have 3 image field for my products, on save method all working fine, but in update method first image saves temp file in database and the other two update without issue.
Code
Here I share all my 3 images on update method:
//saves temp file instead of file name
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageOne)->resize(1200, 600)->save($location);
if(!empty($product->imageOne)){
Storage::delete('images/' . $product->imageOne);
}
$product->imageOne = $imageOne;
}
//works with no issue
if ($request->hasFile('imageTwo')) {
$imageTwo = $request->file('imageTwo');
$filename = 'producttwo' . '-' . time() . '.' . $imageTwo->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageTwo)->resize(1200, 600)->save($location);
if(!empty($product->imageTwo)){
Storage::delete('images/' . $product->imageTwo);
}
$product->imageTwo = $filename;
}
//works with no issue
if ($request->hasFile('imageThree')) {
$imageThree = $request->file('imageThree');
$filename = 'productthree' . '-' . time() . '.' . $imageThree->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($imageThree)->resize(1200, 600)->save($location);
if(!empty($product->imageThree)){
Storage::delete('images/' . $product->imageThree);
}
$product->imageThree = $filename;
}
Any idea?
Issue is here:-
$product->imageOne = $imageOne;
You are saving $imageOne. But you need to save $filename. Use following code:-
$product->imageOne = $filename;

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) }}}

Laravel - Image source not readable after publish website

I was created simple CMS and everything works at localhost. After published website I can't upload image. I have an error "Image source not readable". I changed chmod upload's folder to 777.
My code:
$minphoto_path = 'uploads/img';
$minphoto = $request->minphoto;
$minphoto_new_name = time().$minphoto->getClientOriginalName();
$minphoto->move($minphoto_path, $minphoto_new_name);
$img = Image::make(public_path($minphoto_path . '/' .$minphoto_new_name))->resize(240, 338)->save(public_path($minphoto_path . '/' .$minphoto_new_name));
Try upload that way:
$file = Input::file('file');
$fileName = time() . '-' . $file->getClientOriginalName();
$file->move('uploads', $fileName);
$img = Image::make($file->getRealPath())->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());
Or shorter:
$file = Input::file('file');
$img = Image::make($file)->resize(320, 240)
->save('public/uploads/'. $file->getClientOriginalName());

Resources