Call to a member function getClientOriginalName() on string laravel - laravel

$path = "public/images/";
$Image->banner_image_1 = (is_null($input['banner_image_1']))?($Image->banner_image_1) : ($input['banner_image_1']); $imageName = $Image->banner_image_1->getClientOriginalName();
$file = $Image->banner_image_1;
$file->storeAs($path, $imageName, 's3');
$url = Storage::disk('s3')->url($path.$imageName);
$Image->banner_image_1 = $imageName;
The error Call to a member function getClientOriginalName() on string is occuring while update. How to solve this error?

Related

when i make save in (admin/settings) i got (local.ERROR: Undefined index: utubelink) error

when I make save in (admin/settings) I got (local.ERROR: Undefined index: utubelink) error
it is just in (admin/settings) other admin pages work fine.
I can not make any changes like changing the logo or name of the site.
laravel.log
[2022-01-08 15:46:38] local.ERROR: Undefined index: utubelink {"exception":"[object] (ErrorException(code: 0): Undefined index: utubelink at /home/u0462672/new.maxtella.net/app/Http/Controllers/Admin/Settings.php:44)
Settings.php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Model\Setting;
use Storage;
class Settings extends Controller {
public function setting() {
return view('admin.settings.settings', ['title' => trans('admin.settings')]);
}
public function setting_save() {
$date=$this->validate(request(), [
'logo'=>v_image(),
],[],
[
'logo'=>trans('admin.logo'),
'icon'=>trans('admin.icon')
]);
$data = request()->except(['_token', '_method']);
if(request()->hasFile('logo')) {
$file = request()->file('logo');
$name = str_random(21) . time() . '.' . $file->getClientOriginalExtension();
$ext = $file->getClientOriginalExtension();
$size = $file->getSize();
$mim = $file->getMimeType();
$realpath = $file->getRealPath();
$file->move(public_path('upload/settings/'), $name);
$data['logo'] = $name;
}
if(request()->hasFile('imgindex')) {
$file = request()->file('imgindex');
$name = str_random(21) . time() . '.' . $file->getClientOriginalExtension();
$ext = $file->getClientOriginalExtension();
$size = $file->getSize();
$mim = $file->getMimeType();
$realpath = $file->getRealPath();
$file->move(public_path('upload/settings/'), $name);
$data['imgindex'] = $name;
}
$data['utubelink'] = $data['utubelink'] != '' ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';
Setting::orderBy('id', 'desc')->update($data);
session()->flash('success', trans('admin.updated_record'));
return redirect(aurl('settings'));
}
}
how can fix it plz ?
Ok man Here is the issue.
The error log clear points that line number 44 has an issue. You are trying to access utubelink from $data array which does not exist.
$data['utubelink'] = $data['utubelink'] != '' ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';
Replace with
$data['utubelink'] = ($data['utubelink'] ?? null) ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';
I hope you are using PHP 7.0 or above. Read more about the Null Coalescing Operator in PHP or just use isset().
You can prevent these type of errors using optional Laravel helper method.
$data['utubelink'] = ! blank(optional($data)['utubelink']) ? str_replace("watch?v=", "embed/", $data['utubelink']) : '';
Read more: https://laravel.com/docs/8.x/helpers#method-optional

How to save Base64 string as Image in laravel

I have been searching for the past 2 days now trying to get a solution that decodes base64 in all file type extensions(.png or jpg). All I found was a base64 decoder that only allow one type of extensions.
My Controller:
public function updatepicture(Request $request){
$user = User::find($request->id);
if($user == null){
return response()->json(['statusCode'=>'5', 'statusMessage' => "user account doesn't exists", 'data' => []]);
}
$image = $request->avatar; // your base64 encoded
$decoded_file = base64_decode($image); // decode the file
$mime_type = finfo_buffer(finfo_open(), $decoded_file, FILEINFO_MIME_TYPE); // extract mime type
$extension = $this->mime2ext($mime_type); // extract extension from mime type
$image = str_replace('data:image/'.$extension.';base64,', '', $image);
$image = str_replace(' ', '+', $image);
$filename = str::random(10).'.'.$extension;
//$image = $request->file('avatar');
//$filename = time().'.'.$image->getClientOriginalExtension();
$filePath = 'avatars/'.$filename;
$disk = Storage::disk('gcs')->put($filePath, file_get_contents(base64_decode($image)));
$gcs = Storage::disk('gcs');
$url = $gcs->url('avatars'. "/" . $filename);
$user->avatar = $url;
$user->save();
return response()->json(['statusCode'=>'0', 'statusMessage' => 'Successful','data' => $user], 200);
}
/*
to take mime type as a parameter and return the equivalent extension
*/
public function mime2ext($mime){
$all_mimes = '{"png":["image\/png","image\/x-png"],"bmp":["image\/bmp","image\/x-bmp",
"image\/x-bitmap","image\/x-xbitmap","image\/x-win-bitmap","image\/x-windows-bmp",
"image\/ms-bmp","image\/x-ms-bmp","application\/bmp","application\/x-bmp",
"application\/x-win-bitmap"],"gif":["image\/gif"],"jpeg":["image\/jpeg",
"image\/pjpeg"],"xspf":["application\/xspf+xml"],"vlc":["application\/videolan"],
"wmv":["video\/x-ms-wmv","video\/x-ms-asf"],"au":["audio\/x-au"],
"ac3":["audio\/ac3"],"flac":["audio\/x-flac"],"ogg":["audio\/ogg",
"video\/ogg","application\/ogg"],"kmz":["application\/vnd.google-earth.kmz"],
"kml":["application\/vnd.google-earth.kml+xml"],"rtx":["text\/richtext"],
"rtf":["text\/rtf"],"jar":["application\/java-archive","application\/x-java-application",
"application\/x-jar"],"zip":["application\/x-zip","application\/zip",
"application\/x-zip-compressed","application\/s-compressed","multipart\/x-zip"],
"7zip":["application\/x-compressed"],"xml":["application\/xml","text\/xml"],
"svg":["image\/svg+xml"],"3g2":["video\/3gpp2"],"3gp":["video\/3gp","video\/3gpp"],
"mp4":["video\/mp4"],"m4a":["audio\/x-m4a"],"f4v":["video\/x-f4v"],"flv":["video\/x-flv"],
"webm":["video\/webm"],"aac":["audio\/x-acc"],"m4u":["application\/vnd.mpegurl"],
"pdf":["application\/pdf","application\/octet-stream"],
"pptx":["application\/vnd.openxmlformats-officedocument.presentationml.presentation"],
"ppt":["application\/powerpoint","application\/vnd.ms-powerpoint","application\/vnd.ms-office",
"application\/msword"],"docx":["application\/vnd.openxmlformats-officedocument.wordprocessingml.document"],
"xlsx":["application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet","application\/vnd.ms-excel"],
"xl":["application\/excel"],"xls":["application\/msexcel","application\/x-msexcel","application\/x-ms-excel",
"application\/x-excel","application\/x-dos_ms_excel","application\/xls","application\/x-xls"],
"xsl":["text\/xsl"],"mpeg":["video\/mpeg"],"mov":["video\/quicktime"],"avi":["video\/x-msvideo",
"video\/msvideo","video\/avi","application\/x-troff-msvideo"],"movie":["video\/x-sgi-movie"],
"log":["text\/x-log"],"txt":["text\/plain"],"css":["text\/css"],"html":["text\/html"],
"wav":["audio\/x-wav","audio\/wave","audio\/wav"],"xhtml":["application\/xhtml+xml"],
"tar":["application\/x-tar"],"tgz":["application\/x-gzip-compressed"],"psd":["application\/x-photoshop",
"image\/vnd.adobe.photoshop"],"exe":["application\/x-msdownload"],"js":["application\/x-javascript"],
"mp3":["audio\/mpeg","audio\/mpg","audio\/mpeg3","audio\/mp3"],"rar":["application\/x-rar","application\/rar",
"application\/x-rar-compressed"],"gzip":["application\/x-gzip"],"hqx":["application\/mac-binhex40",
"application\/mac-binhex","application\/x-binhex40","application\/x-mac-binhex40"],
"cpt":["application\/mac-compactpro"],"bin":["application\/macbinary","application\/mac-binary",
"application\/x-binary","application\/x-macbinary"],"oda":["application\/oda"],
"ai":["application\/postscript"],"smil":["application\/smil"],"mif":["application\/vnd.mif"],
"wbxml":["application\/wbxml"],"wmlc":["application\/wmlc"],"dcr":["application\/x-director"],
"dvi":["application\/x-dvi"],"gtar":["application\/x-gtar"],"php":["application\/x-httpd-php",
"application\/php","application\/x-php","text\/php","text\/x-php","application\/x-httpd-php-source"],
"swf":["application\/x-shockwave-flash"],"sit":["application\/x-stuffit"],"z":["application\/x-compress"],
"mid":["audio\/midi"],"aif":["audio\/x-aiff","audio\/aiff"],"ram":["audio\/x-pn-realaudio"],
"rpm":["audio\/x-pn-realaudio-plugin"],"ra":["audio\/x-realaudio"],"rv":["video\/vnd.rn-realvideo"],
"jp2":["image\/jp2","video\/mj2","image\/jpx","image\/jpm"],"tiff":["image\/tiff"],
"eml":["message\/rfc822"],"pem":["application\/x-x509-user-cert","application\/x-pem-file"],
"p10":["application\/x-pkcs10","application\/pkcs10"],"p12":["application\/x-pkcs12"],
"p7a":["application\/x-pkcs7-signature"],"p7c":["application\/pkcs7-mime","application\/x-pkcs7-mime"],"p7r":["application\/x-pkcs7-certreqresp"],"p7s":["application\/pkcs7-signature"],"crt":["application\/x-x509-ca-cert","application\/pkix-cert"],"crl":["application\/pkix-crl","application\/pkcs-crl"],"pgp":["application\/pgp"],"gpg":["application\/gpg-keys"],"rsa":["application\/x-pkcs7"],"ics":["text\/calendar"],"zsh":["text\/x-scriptzsh"],"cdr":["application\/cdr","application\/coreldraw","application\/x-cdr","application\/x-coreldraw","image\/cdr","image\/x-cdr","zz-application\/zz-winassoc-cdr"],"wma":["audio\/x-ms-wma"],"vcf":["text\/x-vcard"],"srt":["text\/srt"],"vtt":["text\/vtt"],"ico":["image\/x-icon","image\/x-ico","image\/vnd.microsoft.icon"],"csv":["text\/x-comma-separated-values","text\/comma-separated-values","application\/vnd.msexcel"],"json":["application\/json","text\/json"]}';
$all_mimes = json_decode($all_mimes,true);
foreach ($all_mimes as $key => $value) {
if(array_search($mime,$value) !== false) return $key;
}
return false;
}
Please help me align this piece of code, the error massage m getting is as follow:
ErrorException: file_get_contents() expects parameter 1 to be a valid path, string given in file
You don't need to use file_get_contents() function while using put method because you are already converting string to image using base64_decode method.
$disk = Storage::disk('gcs')->put($filePath, base64_decode($image));
Please try like this. It should work.

How do grant rw access to public folder in laravel

I get this error after i migrated my project from windows to mac.
The "/private/var/folders/6w/zypn4xb120l6x6f1kjx9_nxw0000gn/T/phpwroBVT" file does not exist or is not readable.
here is my code
if($request->hasFile('image')){
$image = $request->file('image');
$image_name = $image->getClientOriginalName();
$destinationPath = public_path('/images/services');
$image->move($destinationPath, $image_name);
$summary = $request->summary;
$body = $request->body;
$title = $request->title;
$service = Service::create([
'title'=> $title,
'summary'=>$summary,
'body'=>$body,
'image'=> $image
]
);
if($service){
return redirect()->back()->with('success','services added');
}
}
the images goes into the public/images/services folder but i get the above error
I faced the similar error and I found out that i was accessing the global variable declared in the controller without "this" pointer reference. For example
class BlogController extends Controller
{
public $attachment_folder_name = "/blogs/";
}
You should access this variable with following syntax.
echo $this->attachment_folder_name
And not like this
echo $attachment_folder_name

how to fix function getClientOriginalExtension() on null on laravel

check out the code that i am getting error on "Call to a member function getClientOriginalExtension() on bool"
i used image intervention properties on laravel to upload image on product table . while usng getClientOriginalExtension() i got error...
public function product_store(Request $request)
{
$image = $request->hasfile('product_image');
$img = $image->getClientOriginalExtension();
$location = public_path('images/products/' .$img);
Image::make($imge)->save($location);
$product_image = new productImage;
$product_image->product_id = 1;
$product_image->image = $img;
$product_image->save();
return redirect() -> route('admin.product.create');
}
Call to a member function getClientOriginalExtension() on bool
hasFile returning boolean.Try this -
$image = $request->file('product_image');
You can use hasFile() to check if there is a file or not!
if ($request->hasFile('product_image')) {
$image = $request->file('product_image');
}
then get the extension using :
$imageExt = $image->extension();

Call to a member function getClientOriginalName() on a non-object - Laravel 4.2

I am trying to upload an image within my form data
here is the form header
{{ Form::open(array('url'=>'doAddProject', 'file'=>'true', 'method'=>'PUT')) }}
and here is the controller
public function store()
{
$project = new Projects();
$file = Input::file('pImage');
$destination_path = 'images/projects/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$file->move($destination_path, $filename);
$project->main_image = Input::file($filename);
$project->pro_title = Input::get('pName');
$project->pro_map = Input::get('pMap');
$project->pro_description = Input::get('pDetails');
$project->pro_serves = implode(",", array_filter(Input::get('pro_serves')));
$project->pro_activity = Input::get('activity');
$project->save();
return Redirect::to('admin/view-project')->with('message', 'Project add successfully');
}
Got Error :
Call to a member function getClientOriginalName() on a non-object
The Line
$project->main_image = Input::file($filename);
Change it to
$project->main_image = $filename;

Resources