If you want to remove just the files and there aren't any subdirectories:
function emptydir($directory)
{
$files = scandir($directory);
foreach ($files as $value)
{
if ($value != '.' && $value != '..')
{
unlink($directory."/".$value);
}
}
}
If you have sub-directories, this function works well. It'll actually delete the directory. I'm not sure if you want to do that or just empty the directory but you could recreate the directory immediately afterwards.
You only need to pass the directory name when calling the function e.g.
delete(directoryname);
function delete($directory, $empty = FALSE) {
if(substr($directory,-1) == '/')
{
$directory = substr($directory,0,-1);
}
if(!file_exists($directory) || !is_dir($directory))
{
return FALSE;
}elseif(is_readable($directory))
{
$handle = opendir($directory);
while (FALSE !== ($item = readdir($handle)))
{
if($item != '.' && $item != '..')
{
$path = $directory.'/'.$item;
if(is_dir($path))
{
delete($path);
}else{
unlink($path);
}
}
}
closedir($handle);
if($empty == FALSE)
{
if(!rmdir($directory))
{
return FALSE;
}
}
}
return TRUE;
}