LittleMygler
LittleMygler

Reputation: 632

File::deleteDirectory('path') does not remove the directory

I'm trying to remove a folder with images in it.

here is my method for removing the file

public function deleteCar($id) {
    $car = Car::find($id);
    $carImages = carImage::where('car_id', $id);

    foreach ($carImages as $image) {
        //Just for testing purposes.
        $image->car_image_path = '/uploads/cars/32/exampleImage.png';

        $pathWords = explode('/', $image->car_image_path);
        $path = $pathWords[0] . '/' . $pathWords[1] . '/' . $pathWords[2] . '/' . $pathWords[3];
        File::deleteDirectory($path);
        $image->delete();
    }

    $car->delete();

    return response()->json(['error' => false, 'data' => $id]);
}

What I want to accomplish is to remove the folder and then the model with the correct car_id. That is passed.

The folder does not get removed and neither does the image model.

Upvotes: 0

Views: 32

Answers (1)

Serghei Leonenco
Serghei Leonenco

Reputation: 3507

You have to use public_path to delete directory:

File::deleteDirectory(public_path('uploads/cars/32'));

The method will return true if it succeeds, false if it fails. Hope it helps

Upvotes: 1

Related Questions