Skip to content

Instantly share code, notes, and snippets.

@janjouketjalsma
Forked from rveitch/wp_rmdir.php
Last active January 18, 2023 10:12
Show Gist options
  • Save janjouketjalsma/abe1e0eb3df74a9d501bdc21e6830eef to your computer and use it in GitHub Desktop.
Save janjouketjalsma/abe1e0eb3df74a9d501bdc21e6830eef to your computer and use it in GitHub Desktop.
PHP - Delete Files and Folders (wp directory)
<?php
$dir = '/path/to/folder';
if ( is_dir( $dir ) ) {
$it = new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS );
$files = new RecursiveIteratorIterator( $it, RecursiveIteratorIterator::CHILD_FIRST );
foreach ( $files as $file ) {
if ( $file->isDir() ) {
rmdir( $file->getRealPath() );
} else {
unlink( $file->getRealPath() );
}
}
rmdir( $dir );
} else {
echo 'Error: Path is not a valid directory.';
}
echo 'Nuked That Directory.';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment