I'm working on file delete function. I want to delete image from both database and folder. Image is deleting from database but not from the folder. But i want to perform delete image both from database and folder.
Here is my code.

public function dodelete() {
            { 
                $contact = Contact::find(Input::get('id'));
                File::delete("../app/uploads/");
                if(!is_null($contact))
                {
                    $contact->delete($file);
                }
                return Redirect::route('contact');
            }

    }

The path defined in File::delete($path) must include the filename:

$path = '../app/uploads/' . $contact->filename;

Also you should use the helper to set the correct path:

I did it but its not working yet.

Can you show your updated code? Do you got any errors? If in debug mode you should get the stack trace.

Member Avatar for diafol

If you're using L5, then you can use the Storage facade - Storage::delete(). File::delete() if L4.2

commented: +1 +13

Here is my updated code

public function dodelete() {
            { 
                $contact = Contact::find(Input::get('id'));
                $filename= Input::get('upload');
                $path = '../app/uploads/' . $contact->filename;
                if(!is_null($contact))
                {
                    $contact->delete($path);
                }
                return Redirect::route('contact');
            }

    }
Member Avatar for diafol

You don't mention if it works or not. This looks aas though you're deleting the entire contact, not the file. The model delete does not accept an argument AFAIK.

Ok, just add the method to delete the file: follow diafol's suggestion depending the Laravel version you're using.

It is not working. This code is only deleting file from database but not from the folder. I tried so many thing in this function but not able to delete the file from the folder

Here:

if(!is_null($contact))
{
    $contact->delete($path);
}

Place:

if(!is_null($contact))
{
    File::delete($path);
    $contact->delete($path);
}

Or Storage::delete() if using Laravel 5.

Member Avatar for diafol

This is going around in circles. You want to delete the contact AND the uploaded file (if it exists):

function deleteContact($id)
{
    if($contact = Contact::find($id))
    {
        $filename = $contact->filename;
        $fullPath = '../app/uploads/' . $filename;
        if (File::exists($fullPath)) File::delete($fullPath);
        $contact->delete();
        return true;
    }
    return false;
}

if(deleteContact(Input::get('id'))) Redirect::route('contact');
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.