Search code examples
phplaravellaravel-5laravel-5.1

Laravel doesn't delete directory


I have a problem deleting a directory.

I'm creating a temporary directory where I unzip a file and I want to delete it when I'm done with it.

My code looks pretty much like this:

$tempPath = storage_path('temp/'.$filemd5.'/');
Storage::makeDirectory($tempPath);
$success = Storage::deleteDirectory($tempPath);
return $success;

It doesn't return anything and the directory doesn't get deleted.


Solution

  • Maybe you don't have enough permissions on your storage/app directory (which I think is the default one). Check that, and if that doesn't work, you can always appeal to the PHP exec function (assuming that you're on linux):

    exec('rm -r ' . storage_path() . '/' . $tempPath)

    UPDATE: the problem was that makeDirectory has a second int parameter that sets the permissions by default to 511 which is -r-x--x--x (i.e. read and execute, but not write). The workaround is to set the second parameter to 711 which is -rwx--x--x and you'll be able to delete the created directory. It's useful to pass the other two parameter as true because it will apply permissions recursively, and force the operation.

    As Alex says, you have to use File instead of Storage. So the final code for creating directory that later you will be able to delete is:

    File::makeDirectory($tempPath, 0711, true, true);