php remove directory code example

Example 1: php remove file

if(file_exists($file)) {
	unlink($file);
}

Example 2: php delete a folder

function deleteDirectory($dir) {
    if (!file_exists($dir)) {
        return true;
    }

    if (!is_dir($dir)) {
        return unlink($dir);
    }

    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }

        if (!deleteDirectory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }

    }

    return rmdir($dir);
}

Example 3: php delete directory

rmdir ( string $dirname , resource $context = ? ) : bool

Tags:

Php Example