Sid Heart
Sid Heart

Reputation: 743

The image must be a file of type: jpeg, bmp, png, jpg. in laravel 5.5

I don't understand why it's not working and getting me error

here is my form

{!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'true']]) !!}

                      <div class="form-group{{ $errors->has('title') ? ' has-error' : '' }}">
                    {!! Form::label('title', 'Title') !!}
                    {!! Form::text('title', $project->title, ['class' => 'form-control', 'required' => 'required']) !!}
                    <small class="text-danger">{{ $errors->first('title') }}</small>
                </div>
                <div class="form-group{{ $errors->has('content') ? ' has-error' : '' }}">
                    {!! Form::label('content', 'Content') !!}
                    {!! Form::textarea('content', $project->content, ['class' => 'form-control', 'required' => 'required']) !!}
                    <small class="text-danger">{{ $errors->first('content') }}</small>
                </div>

                @if($project->progress == 1)
                <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                    <label for="progress">
                        {!! Form::radio('progress', '1',  null, ['id' => 'radio_id', 'checked' => 'checked']) !!} In Progress
                    </label>
                    <small class="text-danger">{{ $errors->first('progress') }}</small>
                </div>

                <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                    <label for="progress">
                        {!! Form::radio('progress', '2',  null, ['id' => 'radio_id']) !!} Complete
                    </label>
                    <small class="text-danger">{{ $errors->first('progress') }}</small>
                </div>
                @else
                <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                    <label for="progress">
                        {!! Form::radio('progress', '1',  null, ['id' => 'radio_id']) !!} In Progress
                    </label>
                    <small class="text-danger">{{ $errors->first('progress') }}</small>
                </div>

                <div class="radio{{ $errors->has('progress') ? ' has-error' : '' }}">
                    <label for="progress">
                        {!! Form::radio('progress', '2',  null, ['id' => 'radio_id', 'checked' => 'checked']) !!} Complete
                    </label>
                    <small class="text-danger">{{ $errors->first('progress') }}</small>
                </div>
                @endif

                <div class="form-group{{ $errors->has('image') ? ' has-error' : '' }}">
                    {!! Form::label('image', 'Select Image of page') !!}
                    {!! Form::file('image') !!}
                    <p class="help-block">for better view select 1920x1080 size of image</p>
                    <small class="text-danger">{{ $errors->first('image') }}</small>
                </div>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      {!! Form::submit("Edit", ['class' => 'btn btn-warning pull-right']) !!}

                  {!! Form::close() !!}

And my admin panel

public function projectedit($id, Request $request){

    $this->validate($request, [
        'title' => 'required|max:255',
        'content' => 'required|max:10000',
        'image' => 'mimes:jpeg,bmp,png,jpg',
    ]);

    $project = Project::where('slug', $id)->firstorfail();
    $project->title = $request->title;
    $project->slug = str_slug($project->title, '-');
    $project->content = $request->content;
    $project->progress = $request->progress;
    if($request->hasFile('image')) {
        $file = Input::file('image');
        //getting timestamp
        $timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
        $name = $timestamp. '-' .$file->getClientOriginalName();
        $file->move(public_path().'/images/project/', $name);
        $project->image = $name;
        $thumb = Image::make(public_path().'/images/project/' . $name)->resize(1200,500)->save(public_path().'/images/project/thumb/' . $name, 60);
    }
    $project->save();
    return Redirect::back()->with('status', 'Project Edit Success');

}

Please tell me what I'm doing wrong everything is working fine but image not uploading and not uploading image only else everything is working fine.

Upvotes: 1

Views: 12143

Answers (5)

Yubaraj Shrestha
Yubaraj Shrestha

Reputation: 884

Change following line:

{!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'false']]) !!}

to this:

{!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug], 'files' => 'true']) !!}

And the way you are uploading image is quite messy it looks in controller how about using Laravel Filesystem for that?

Change following line in config/filesytems.php:

'local' => [
     'driver' => 'local',
     'root' => storage_path('app'),
],

to this:

'local' => [
     'driver' => 'local',
     'root' => public_path(),
],

Then in the controller you can do this:

public function projectedit($id, Request $request){
    $this->validate($request, [
        'title' => 'required|max:255',
        'content' => 'required|max:10000',
        'image' => 'mimes:jpeg,bmp,png,jpg',
    ]);

    $project = Project::where('slug', $id)->firstorfail();
    $project->title = $request->title;
    $project->slug = str_slug($project->title, '-');
    $project->content = $request->content;
    $project->progress = $request->progress;
    if($request->hasFile('image')) {
        $project->image = $request->file('image')->store('images/project');
    }
    $project->save();
    return Redirect::back()->with('status', 'Project Edit Success');
}

Clean right? :)

Upvotes: 1

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

Please change your form method like:

{!! Form::open(['method' => 'PUT', 'route' => ['admin.project.edit', $project->slug, 'files' => 'false']]) !!}

To

{!! Form::open(['method' => 'POST', 'route' => ['admin.project.edit', $project->slug, 'files' => 'true']]) !!}

Upvotes: 0

Viraj Shah
Viraj Shah

Reputation: 379

set 'files' => 'false' to 'files' => 'true' in your form.

and check your php.ini file for the file_upload=on;

Upvotes: 0

kofoworola
kofoworola

Reputation: 557

In your form creation code you set:

['files' => 'false']

This restricts file upload, when you change it to true, the generated form will have a encoding type of

enctype= multipart/form-data laravel

Upvotes: 0

Insax
Insax

Reputation: 622

You should set

'files' => 'false'

to

'files' => 'true'

in Line 1 of your Form

Upvotes: 2

Related Questions