<?php
$dir = $modx->getOption('base_path') . 'assets/p-images/'; //original images
$files = glob($dir . "*.jpg"); //get all jpg from dir
$id = "/" . $id . "-"; //make sure if this is really correct number in /xxx-name.jpg
foreach($files as $image) {
if (strpos($image,$id)) { //if correct string in path...
unlink($image); //delete image in p-images
$image = str_replace("p-images", "w-images", $image); //change path
unlink($image); //delete image in w-images
}
}
return;<?php
/** recursive remove dir function */
function rrmdir($dir) {
if (is_dir($dir)) {
$objects = scandir($dir);
foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
$prefix = substr($object, 0, 4);
if ($prefix == '.git' || $prefix == '.svn') {
continue;
}
rrmdir($dir . "/" . $object);
} else {
$prefix = substr($object, 0, 4);
if ( $prefix != '.git' && $prefix != '.svn') {
@unlink($dir . "/" . $object);
}
}
}
}
reset($objects);
$success = @rmdir($dir);
}
}
$path = $modx->getOption('base_path') . '/assets/content/resource/'; // replace with actual path to directory
$path .= $id;
rrmdir($path);foreach ($objects as $object) {
if ($object != "." && $object != "..") {
if (filetype($dir . "/" . $object) == "dir") {
rrmdir($dir . "/" . $object);
} else {
@unlink($dir . "/" . $object);
}
}
}I'm not sure what I was thinking almost two years ago when I wrote that, but it appears that it will fail on any directory that contains a .git or .svn file.
You probably don't have any, which is why it's working, but it would be little faster if you replaced the foreach loop with this:
foreach ($objects as $object) { if ($object != "." && $object != "..") { if (filetype($dir . "/" . $object) == "dir") { rrmdir($dir . "/" . $object); } else { @unlink($dir . "/" . $object); } } }