cugurel's avatar

I can not upload website logo

Hello dear, i want to upload my website logo to file but it says unvalid file extension. I tried this, can you please help me? :)

public function post_settings(Request $request) { if(isset($request->logo)) { $validator=Validator::make($request->all(),[ 'logo'=>'mimes:jpg,jpeg,gif,png', ]);

    if($validator->fails())
    {
       return response(['situation'=>'error','title'=>'There is an error','content'=>'Unvalid file extension']);
    }
    $logo=Input::file('logo');
    $logo_extension=Input::file('logo')->getClientOrriginalExtension();
    $logo_name='logo.'.$logo_extension;
    Storage::disk('uploads')->makeDirectory('img');
     Image::make($logo->getRealPath())->resize(222,108)->save('uploads/img/'.$logo_name);
    }
    
    try{
        unset($request['_token']);
        if(isset($request->logo))
        {
            unset($request['old_logo']);
            Setting::where('id',1)->update($request->all());
            Setting::where('id',1)->update(['logo'=>$logo_name]);
        }
         else{
            $old_logo=$request->old_logo;
            unset($request['old_logo']);
            Setting::where('id',1)->update($request->all());
            Setting::where('id',1)->update(['logo'=>$old_logo]);
         }
        return response(['situation'=>'success','title'=>'Succesful','content'=>'Process is completely done!']);
        }
        
    catch (\Exception $e)
    {
        return response(['situation'=>'error','title'=>'There is an error','content'=>'Process is okay!!!!']);
    }
}
0 likes
18 replies
rin4ik's avatar

try this please

$logo_extension=Input::file('logo')->extension();

or dd after this what you get?

$logo_extension=Input::file('logo')->getClientOrriginalExtension();
dd($logo_extension);
tykus's avatar

What error message are you actually getting?

Also, getClientOrriginalExtension is a typo? Should be getClientOriginalExtension

cugurel's avatar

Unfortunately after edit, i got same result. :/

tykus's avatar

i got same result.

What result - what is the error message?

cugurel's avatar

@tykus

This JSON file does not appear to have any style information associated with it. The document tree is shown below. Object content:"Unvalid file extension" situation:"error" title:"There is an error"

rin4ik's avatar

try this instead


public function post_settings(Request $request) { 
   if(isset($request->logo)) {
$request->validate([
            'logo' => 'mimes:jpg,jpeg,gif,png',
        ]);

    $logo=Input::file('logo');
    $logo_extension=Input::file('logo')->getClientOriginalExtension();
    $logo_name='logo.'.$logo_extension;
    Storage::disk('uploads')->makeDirectory('img');
     Image::make($logo->getRealPath())->resize(222,108)->save('uploads/img/'.$logo_name);
    }
    
    try{
        unset($request['_token']);
        if(isset($request->logo))
        {
            unset($request['old_logo']);
            Setting::where('id',1)->update($request->all());
            Setting::where('id',1)->update(['logo'=>$logo_name]);
        }
         else{
            $old_logo=$request->old_logo;
            unset($request['old_logo']);
            Setting::where('id',1)->update($request->all());
            Setting::where('id',1)->update(['logo'=>$old_logo]);
         }
        return response(['situation'=>'success','title'=>'Succesful','content'=>'Process is completely done!']);
        }
        
    catch (\Exception $e)
    {
        return response(['situation'=>'error','title'=>'There is an error','content'=>'Process is okay!!!!']);
    }
}
cugurel's avatar

@rin4ik

(1/1) BadMethodCallException Method validate does not exist.

It is giving this error. :(

rin4ik's avatar

ok change

$request->validate([
            'logo' => 'mimes:jpg,jpeg,gif,png',
        ]);

to

$this->validate($request, [
 'logo' => 'mimes:jpg,jpeg,gif,png',
        ]);
ekhlas's avatar

which file you upload

give filename which you want to upload

cugurel's avatar

@ekhlas shopio-logo.png and @rin4ik it doesn't work. In my past project it worked bu now, the codes same and and working.

rin4ik's avatar

@cugurel like I said earlier remove validation at all and try for testing. logo in db stored with name logo? it doesn't pass the validation

ekhlas's avatar

give 777 to upload folder permission

may be you not give proper folder permission.

cugurel's avatar

Okay never mind. Almost i tried all possible solutions. Maybe i will do it in one day but i will do it.

tykus's avatar

Unvalid is not a word I am familiar with... is this something you have written because I suspect it is not coming from Laravel.

Also, your responses appear to be JSON; are you uploading using XHR request? You would need to use the FormData API to upload a file asynchronously.

rin4ik's avatar

@tykus it is the validation error he defined

public function post_settings(Request $request) 
{
 if(isset($request->logo))
 {
 $validator=Validator::make($request->all(),[ 'logo'=>'mimes:jpg,jpeg,gif,png', ]);

    if($validator->fails())
    {
       return response(['situation'=>'error','title'=>'There is an error','content'=>'Unvalid file extension']);
    }
cugurel's avatar

Unvalid only a string for validation error.

Please or to participate in this conversation.