Skip to content

Instantly share code, notes, and snippets.

@martin-klima
Created April 4, 2023 16:07
Show Gist options
  • Save martin-klima/89121bf1fd1833c74cb1803f2b8a79d6 to your computer and use it in GitHub Desktop.
Save martin-klima/89121bf1fd1833c74cb1803f2b8a79d6 to your computer and use it in GitHub Desktop.
Delete all files inside of a directory.
/**
* Delete all content inside of given directory.
*
* @param string $folder
* Directory.
*/
public function deleteAllFilesInsideDirectory(string $folder): void {
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($folder, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
if ($fileinfo->isDir()) {
rmdir($fileinfo->getRealPath());
}
else {
unlink($fileinfo->getRealPath());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment