Remove directory and all its content in PHP

To recursively delete all the contents in the directory and then the directory itself. This function checks for files and sub directories and delete them first before deleting the main folder.

$directory      = "uploads";
 
if(is_dir($directory)){
	recursiveRemoveDirectory($directory);
}
 
function recursiveRemoveDirectory($directory) {
    foreach(glob("{$directory}/*") as $file) {
        if(is_dir($file)) {
            recursiveRemoveDirectory($file);
        } else {
            unlink($file);
        }
    }
    rmdir($directory);
}

Confirm before Deleting post in CakePHP

In the controller, create a delete function:

<?php
class EmployeeController extends AppController
{
public function delete($id)
{
$this->Employee->delete($id);
$this->Session->setFlash(‘The Employee is deleted.’);
$this->redirect(array(‘action’=>’index’));
}
}

In the view page, write the following code:

<a href=”#” onclick=”return confirm(‘Are you sure you want to delete this Employee?’);”>Delete</a>