Skip to content

Instantly share code, notes, and snippets.

@davidpolanco
Last active June 27, 2020 23:35
Show Gist options
  • Save davidpolanco/3358294a790d025eb8988fdd8ad09977 to your computer and use it in GitHub Desktop.
Save davidpolanco/3358294a790d025eb8988fdd8ad09977 to your computer and use it in GitHub Desktop.
Delete Files inside a Custom Directory #php #wordpress
<?php
/*
*
* @author David Polanco
*
* https://davidpolanco.com
* https://twitter.com/davidpolanco
* Sources for this can be found at:
* https://github.com/davidpolanco
*/
/* create a directory mine is uploads */
define('PATH', 'uploads/');
function destroy($dir) {
$mydir = opendir($dir);
while(false !== ($file = readdir($mydir))) {
if($file != "." && $file != "..") {
/* set permissions for the directory and file */
chmod($dir.$file, 0775);
if(is_dir($dir.$file)) {
chdir('.');
/* delete directory and files inside of uploads */
destroy($dir.$file.'/');
rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
else
unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
}
}
closedir($mydir);
}
destroy(PATH);
echo 'all done.';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment