Last active
June 27, 2020 23:35
-
-
Save davidpolanco/3358294a790d025eb8988fdd8ad09977 to your computer and use it in GitHub Desktop.
Delete Files inside a Custom Directory #php #wordpress
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
<?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