how can i delete the directory and its all the files belonging in it with php?
rmdir — Removes directory
bool rmdir ( string $dirname [, resource $context ] )
Attempts to remove the directory named by dirname . The directory must be empty, and the relevant permissions must permit this.
Note
rmdir will not remove the directory if the directory is not empty and you will receive a warning message. You would either have to loop through and unlink any existing files, or you could invoke a system command on the directory.
Eg:
<?php
//Delete folder function
function deleteDirectory($dir) {
if (!file_exists($dir)) return true;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $item) {
if ($item == '.' || $item == '..') continue;
if (!deleteDirectory($dir . "/" . $item)) {
chmod($dir . "/" . $item, 0777);
if (!deleteDirectory($dir . "/" . $item)) return false;
};
}
return rmdir($dir);
}
?>
You want to nuke a directory and it's contents (which means phoenix's answer is incomplete, since rmdir() won't remove a non-empty directory.
If you're being quick-n-dirty, just use whatever facility the system provides. Backticks will make this easy.:
<?PHP
`rm -rf /some/directory/i/hate`
?>
Or you could use exec() (and see the "See Also" options on that page)
another version using recursiveDirectoryIterator, removes a directory including any files/folders within.
<?php
function RmDirRecursive($path)
{
$realPath = realpath($path);
if (!file_exists($realPath))
return false;
$DirIter = new RecursiveDirectoryIterator($realPath);
$fileObjects = new RecursiveIteratorIterator($DirIter, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($fileObjects as $name => $fileObj) {
if ($fileObj->isDir())
rmdir($name);
else
unlink($name);
}
rmdir($realPath);
return true;
}
?>