Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why update picture doesn't detect file uploaded?

I am using laravel 5 to create an edit form for a profile and can update picture in the form.

I want to store a new image in edit form. I use this code in edit.blade to get the image by user.

View:

{!! Form::model($dataItemregistration,['method' => 'PATCH', 'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID, 'files' => true] ]) !!}
<div class="form-group">
    <div class="row">
        <div class="col-lg-3"> 
            {{ Form::label('pic', 'Gambar (Saiz gambar, 250x300px)') }}
        </div>
        <div class="col-lg-7">
            {!! Form::file('gambar', array('class' => 'form-control')) !!}
        </div>
    </div>
</div>
<br>
<div class="col-lg-10 text-center">
   {!! link_to(URL::previous(),'Back', ['class' => 'btn btn-warning btn-md']) !!}
   {{ Form::submit('Update', ['class' => 'btn btn-primary']) }}
</div>
{!! Form::close() !!}

Controller:

public function update(Request $request, $id)
{
    $valueitemregistrations = Itemregistration::find($id);

    $this->validate($request,['gambar' => 'max:100000',]);
    if ($request->hasFile('gambar')) {
        // Get the file from the request
        $file = $request->file('gambar');
        // Get the contents of the file
        $content = $file->openFile()->fread($file->getSize());

        $valueitemregistrations->Picture = $content;
        $valueitemregistrations->update();

        if($valueitemregistrations) {
            return redirect('profil');
        } else {
            return redirect()->back()->withInput();
        }
    } else { 
        echo "testing";
    }
}

When I try to upload and update, it goes to echo "testing". It doesn't detected any files uploaded..

I had been using the same code for add.blade and it works.

Is it related to route path or else?

like image 740
joun Avatar asked Oct 30 '18 02:10

joun


People also ask

How do you fix the uploaded file could not be moved to wp content uploads?

Try changing the permissions on the wp-content directory to 766. If you still have problems, try 767, 775 or 777. Once your uploads are working, change the permissions on wp-content back to 755 again and check that everything still works OK.

How do you check if an uploaded file is an image or not?

Just check if it starts with image/ . String fileName = uploadedFile. getFileName(); String mimeType = getServletContext(). getMimeType(fileName); if (mimeType.


1 Answers

This happens when your HTML form doesn't have enctype="multipart/form-data".

In this case the cause is 'files' => true being part of the wrong array inside Form::model(); it's inside the 'action' array when it should be outside. Try this:

Form::model($dataItemregistration, [
    'method' => 'PATCH',
    'action' => ['Modul\ProfilController@update', $dataItemregistration->ItemRegistrationID],
    'files' => true,
]);
like image 68
Travis Britz Avatar answered Oct 21 '22 07:10

Travis Britz