0

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');
            }

    }
3
Contributors
10
Replies
48
Views
2 Years
Discussion Span
Last Post by diafol
Featured Replies
  • 1
    diafol 3,669   2 Years Ago

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

  • 1
    diafol 3,669   2 Years Ago

    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'); Read More

0

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

1

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

Edited by diafol

Votes + Comments
+1
0

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');
            }

    }
0

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.

Edited by diafol

0

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

0

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

0

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.

1

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');
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.