Created
April 4, 2023 16:07
-
-
Save martin-klima/89121bf1fd1833c74cb1803f2b8a79d6 to your computer and use it in GitHub Desktop.
Delete all files inside of a directory.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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