durairaj's avatar

The image must be an image.

The image must be an image. this error shows on only live server otherwise localhost it is uploading files

In form.blade.php

<form method="POST" action="{{ route( 'slider.store' ) }}" enctype="multipart/form-data">
    
                @csrf

                <div class="col-sm-6">
        <div class="form-group">

            <label for="image">
                Image <span class="text-danger">*</span>
            </label>
            <input type="file" name="image" class="form-control-file" />

            @if( isset( $slider_info ) && $slider_info->image )
                <div>
                    <img style="width: 100px; height: 50px;" src="{{ asset( 'storage/' . $slider_info->image ) }}" />
                </div>
            @endif

        </div>
    </div>
                <div>
                    <button type="submit" name="create_slider" class="btn btn-sm btn-success">Create Slider</button>
                    <a class="btn btn-sm btn-danger" href="{{ route( 'slider.index' ) }}">Back</a>
                </div>

            </form>

In controller

public function store( Request $request ) {
        
        $rules[ 'title' ]               = 'required';
        $rules[ 'image' ]               = 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048';
        $post_data[ 'title' ]           = $request->title;
        
        if( $request->hasFile( 'image' ) ) {
            $fileData                   = $request->only( 'image' );
            $file                       = $fileData[ 'image' ];
            $post_data[ 'image' ]       = $file;
        }

        $validator                  = Validator::make( $post_data, $rules );
        
        if ( $validator->fails() ) {

            return redirect()->back()->with( 'errors', $validator->errors() )->withInput();

        } else {

            if( $request->hasFile( 'image' ) ) {
                $file                           = $request->file( 'image' )->store( 'slider' );
                $validated_data[ 'image' ]      = $file;
            }

            $validated_data[ 'title' ]          = Input::get( 'title' );
            

            Slider::create( $validated_data );


            return redirect()->back()->with( 'status', 'Slider added successfully!' );

        }

    }

any type image uploading this error will come.

pls tell me how to solve this problem

0 likes
1 reply
durairaj's avatar
durairaj
OP
Best Answer
Level 1

after somany search i found solution

vendor\laravel\framework\src\Illuminate\Validation\Concerns\ValidatesAttributes.php

public function validateImage($attribute, $value)
    {
        return $this->validateMimes($attribute, $value, [ 'jpg', 'jpeg', 'png', 'gif', 'bmp', 'svg', 'webp' ]);
    }

here i added jpg in first it will upload images.

thanks for helping me

2 likes

Please or to participate in this conversation.