Jump to content

Deleting Directories


MoFish

Recommended Posts

Hi,

 

I'm using the following code to delete all directories in the root of my website which works well... however..

 

I'm wanting to amend the code so it does not delete a folder called "install" and also does not delete a file called "configuration.php"

 

Could anyone please advise me on how i could exclude a folder and file?

 

Thanks,

 

MoFish

	function deleteDirectory($dirPath) {
	    if (is_dir($dirPath)) {
	        $objects = scandir($dirPath);
	        foreach ($objects as $object) {
	            if ($object != "." && $object !="..") {
	                if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
	                    deleteDirectory($dirPath . DIRECTORY_SEPARATOR . $object);
	                } else {
	                    unlink($dirPath . DIRECTORY_SEPARATOR . $object);
	                }
	            }
	        }
	    reset($objects);
	    rmdir($dirPath);
	    return true;
	    }
	}
Link to comment
https://forums.phpfreaks.com/topic/287765-deleting-directories/
Share on other sites

Hi,

 

@Jazzman - Yes, its using Linux.

 

The folder needs to be writeable via code, but running the deleteDirectory() function should NOT remove the /install folder or file configuration.php.

 

I thought there would of been a simple way to exclude a folder name / file name in the function itself - but am happy to go down the permission route if its better. I'm just not sure what to set it too.

 

@MECU - will chmod 000 allow me to still read/write to the folder?

  Quote

 

The folder needs to be writeable via code, but running the deleteDirectory() function should NOT remove the /install folder or file configuration.php.

 

Then you will need to add/create a new user who will belong to the root's group. So, for all users belong to root's group you'll need to set special permissions on those directories/files. Don't use a root account to create, modify or delete whatever you want, use this account only in emergency case. What linux distro is that?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.