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
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?

Edited by MoFish
Link to comment
Share on other sites

 

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.