ezraarta's avatar

can't upload file when edit user

hello, i want to edit user profile picture. i've success upload foto in register form, but when i edit user from edit form image won't upload to server(public/uploads).

this is create method :

public function store(CreateDosenRequest $request)
{
    $user = User::create([
        'name'     => $request->input('name'),
        'username' => $request->input('username'),
        'email'    => $request->input('email'),
        'password' => $request->input('password'),
        'admin'    => $request->input('admin'),
    ]);
    if (Input::hasFile('image')) {
        $data        = Input::file('image');
        $photo       = Input::file('image')->getClientOriginalName();
        $destination = public_path() . '/uploads/';
        Request::file('image')->move($destination, $photo);
        $data = $photo;
    }

    $dosen = Dosen::create([
        'iddosen'           => $request->input('iddosen'),
        'nipy'              => $request->input('nipy'),
        'namadosen'         => $user->name,
        'user_id'           => $user->id,
        'alamatdosen'       => $request->input('alamatdosen'),
        'notelpdosen'       => $request->input('notelpdosen'),
        'tempatlahirdosen'  => $request->input('tempatlahirdosen'),
        'tanggallahirdosen' => $request->input('tanggallahirdosen'),
        'agamadosen'        => $request->input('agamadosen'),
        'fotodosen'         => $data, //you have to add it hear

    ]);

    return redirect('admin/dosen')->with('message', 'Data berhasil ditambahkan!');

}

edit form method :

public function update(Request $request, $id)

    {


        if (Input::hasFile('fotodosen')) {
        $data        = Input::file('fotodosen');
        $photo       = Input::file('fotodosen')->getClientOriginalName();
        $destination = public_path() . '/uploads/';
        Request::file('fotodosen')->move($destination, $photo);
        $data['fotodosen'] = $photo;
    }
        $dosenUpdate = Request::only(['nipy', 'namadosen', 'alamatdosen', 'notelpdosen', 'tempatlahirdosen', 'tanggallahirdosen', 'statusdosen', 'fotodosen']);
        $user = User::find($id);
        $user->dosen()->update($dosenUpdate);
        if(Auth::user()->admin==1) {
            return redirect('/admin/dosen')->with('message', 'Data berhasil diubah!');
        }

        return redirect('/dosen')->with('message', 'Data berhasil diubah!');


    }

edit view :

<form class="form-horizontal" method="post" action="{{ url('/admin/dosen') }}" enctype="multipart/form-data">
                                <div class="form-group">
                                <label for="photo">Upload Photo</label>
                            
                                    <input type="file" name="fotodosen" class="form-control">
                                
                         </div>

                                {{ Form::button('<i class="fa fa-check-square-o"></i> Simpan', ['type' => 'submit', 'class' => 'btn btn-primary'] )  }}
                                <a class="btn btn-small btn-success" href="{{ URL('dosen/') }}"><i class="fa fa-reply"></i> Kembali</a>

                            {!! Form::close() !!}
                            </form>

what am i missing?

0 likes
2 replies
Snapey's avatar

Are you relying on the name of the file? How can you stop one user overwriting another?

The problem is probably that you cannot write over the top of the existing file.

You should set the filename to be something based on the user id and then delete any previous file before saving the new one.

I would also take steps to ensure the file is an image since as far as I can see, there is nothing to stop the user uploading badscript.php and then executing it.

Please or to participate in this conversation.