6

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.'
    );
}
tereško
  • 58,060
  • 25
  • 98
  • 150
patrick
  • 881
  • 2
  • 9
  • 32
  • This has nothing to do with the request method. Can you post the complete code of the Controllers method that handles the post request? – chris342423 Jan 15 '14 at 14:35
  • 1
    access rights would be my first guess. second: why are you actually have to delete the file. simply overwrite it. – Kjell Jan 15 '14 at 14:47
  • The old file is not deleted there? – chris342423 Jan 15 '14 at 14:48
  • I have not implemented the code jet because it did not work. Will do now. Have a look please. – patrick Jan 15 '14 at 15:19
  • Access rights? Hm. But I can delete the file with the code I have if I put it on a blank page...??? – patrick Jan 15 '14 at 15:20
  • What contains your $oldimage = Auth::user()->profile_picture; ? – Needpoule Jan 15 '14 at 17:12
  • You are not using public_path() when deleting the old image, maybe you want to add it. Also, you are using $user = User::find($id) AND Auth::user(), maybe there are not always the same? – chris342423 Jan 15 '14 at 22:40
  • The $oldimage contains the image name: test.png – patrick Jan 15 '14 at 23:22
  • The thing is that those two lines of code DO work. If I set up a simple route and paste them into the function this works fine and Laravel deletes the images. It just does not want to work in combination with creating the new one. :( – patrick Jan 15 '14 at 23:24
  • Well @chris342423 in this case they definitely have to be the same. – patrick Jan 15 '14 at 23:26
  • @chris342423 even public path does not do anything. – patrick Jan 15 '14 at 23:32

2 Answers2

1

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);
}
cecilozaur
  • 695
  • 5
  • 6
  • I did not have a problem with the those to lines. They simply did not work in the update function. As I sourced them out to a helper function and called this function instead everything worked out fine. – patrick Jan 17 '14 at 09:10
0

I found a way to do this by creating a helper function an then calling the helper function in the controller method.

http://laravel.com/docs/helpers

patrick
  • 881
  • 2
  • 9
  • 32