PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, February 10, 2022

[FIXED] Laravel doesn't delete directory

 February 10, 2022     laravel, laravel-5, laravel-5.1, php     No comments   

Issue

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


Answered By - soutoner
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing