Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lara_Love's avatar

remove the image from inside hosting

Hello to all dear friends We always save an image for a product in the database and delete the product after a while, but the image remains on the host. The only way to remove the image from inside hosting is to go hosting the panel, is there?

0 likes
13 replies
jlrdw's avatar

The image name should match the fiels name, then just look for the image and delete (unlink). Meaning find in code and delete.

Once in correct folder:

if (is_file($tmpfile)) {
    // code here to unlink

Edit:

As shown below by @benjamin1509 you can find all this stuff in the documentation and API.

1 like
NoLAstNamE's avatar

You can remove the image after deleting a product.

...

if (! is_null($product->image_path)) {
    if (Storage::disk('local')->exists($product->image_path)) {
        Storage::disk('local')->delete($product->image_path);
    }
    // ...or
    unlink($product->image_path);
}
Lara_Love's avatar

how do it work Store Function is

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'active' => 'required',
            'desc' => 'required',
            'ok' => 'required',
            'price' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:500',
        ]);
        $input = $request->all();
        if ($image = $request->file('image')) {
            $destinationPath = 'image/product/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image'] = "$profileImage";
        }
        $input['user_id'] = Auth::id();
        $category = Category::findOrFail($request->category_id);
        $category->products()->create($input);
        return redirect('item')
            ->with('success', 'OK ');
    }

destroy had error

public function destroy($id)
    {
        $item = Product::findOrFail($id);
        $item->delete();
        if (! is_null($item->image/product/)) {
            if (Storage::disk('local')->exists($item->image_path)) {
                Storage::disk('local')->delete($item->image_path);
            }
            // ...or
           // unlink($item->image_path);
        }
        return redirect()->route('item.index')
            ->with('success', 'OK');
    }
Lara_Love's avatar

@Sinnbeck where it stored and where we want delete it from host.destroy not work and its code is amazing

NoLAstNamE's avatar
Level 8

@LoverCode My English is not good too but it's quite hard to understand your replies.

public function destroy($id)
{
    $item = Product::findOrFail($id);
    $imagePath = public_path().'/'.$item->image_path;

    unlink($imagePath);

    if ($item->delete()) {
        return redirect()->route('item.index')
            ->with('success', 'OK');
    }

    return redirect()->route('item.index')
        ->with('error', 'Failed');
}
Lara_Love's avatar

@benjamin1509 it like our code

public function destroy($id)
    {
        $item = Slider::findOrFail($id);
        $item->delete();
        return redirect()->route('slider.index')
            ->with('success', 'OK');

it cannot remove picture from folder hosting

we are like together . i am some times just write and our friends take error and solve my problem

NoLAstNamE's avatar

@LoverCode Did you check the code that I posted? that code with your recent comment is not using it.

EDIT also now your code changed, from Product to Slider now.

$item = Slider::findOrFail($id);
Lara_Love's avatar

@jlrdw Hello . Thanks for your answer/ my focus now is on the ticket system. But again, thanks for reading my question/ with your permission to give the best rating to our friend who tried hard to solve the problem.

Please or to participate in this conversation.