Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4, delete image within a post method / possible?

I am trying to delete an old user image, if the user updates his profile picture. I am using Laravel 4.1 and a resourceful UsersController.

Updating the picture works perfectly fine. A new one is saved in my folder and the file nave is overwritten in the database. However, I would like to delete the old image. Therefore I have the following code, which works fine if I use it on an empty page with route get 'test' for example

$oldimage = Auth::user()->profile_picture;

File::delete('img/profile_pictures/users/' . $oldimage);

Everytime I try to implement this into the process of updating the image, the old one is not deleted. I have already in mind that I have to delete the old one before overwriting the file name.

Does this have to do anything with the POST method the Controller uses for updating? How could I fix it?

public function update($id){
    $validation = Validator::make(
        Input::all(), [
            'name'=>'required', 
            'surname'=>'required', 
            'email'=>'required',
            'email' => 'email']);

    if($validation->fails()){
        return Redirect::to(
                'dashboard#profile'
            )->withInput()->withErrors(
                $validation->messages());
    }

    $user = User::find($id);
    $user->name = Input::get('name');
    $user->surname = Input::get('surname');
    $user->email = Input::get('email');

    if (Input::hasFile('profile_picture_update')) {
        $oldimage = Auth::user()->profile_picture;
        File::delete('img/profile_pictures/users/' . $oldimage);
    }

    $imageFile = Input::file('profile_picture_update');
    $newprofile_picture = Image::make(
        $imageFile->getRealPath()
    )->resize(400, null, true)->crop(400, 400);

    $name = time() . '-' . 
        Input::get('name') . 
        '-' . Input::get('surname') . 
        '.' . $imageFile->getClientOriginalExtension();

    // $name = time() . '-' . $profile_picture->getClientOriginalName();

    // Below the profile_picture variable is overrridden.
    $newprofile_picture = $newprofile_picture->save(
        public_path().'/img/profile_pictures/users/'.$name
    );
    $user->profile_picture = $name;
    $user->save();

    return Redirect::to(
        'dashboard#profile'
    )->with(
        'updatemessage', 'Yep! Deine Änderungen wurden gespeichert.'
    );
}
like image 817
patrick Avatar asked Jan 15 '14 13:01

patrick


1 Answers

why don't you use the User model you just retrieved to get the old image?

if (Input::hasFile('profile_picture_update')) {
    $oldimage = Auth::user()->profile_picture;
    File::delete('img/profile_pictures/users/' . $oldimage);
}

replace it with

if (Input::hasFile('profile_picture_update')) {
    $oldimage = $user->profile_picture;
    File::delete('img/profile_pictures/users/' . $oldimage);
}
like image 53
cecilozaur Avatar answered Oct 19 '22 15:10

cecilozaur