I have "simple file upload" store it's files in a folder with the id of the current resource.
Like assets/19/
I wonder how to delete that folder including all files inside automatically when I delete that resource.
Is there already a plugin that can do this?
I think a plugin connected to OnResourceDelete with this code would do it:
<?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 = 'path/to/parent/directory/'; // replace with actual path to directory
$path .= $id;
rrmdir($path);
I would create a snippet with this code first, with the $id hard-coded at the top, to test it before creating the plugin. You can put echo statements in a snippet to help you debug it, but not in a plugin.
[ed. note: BobRay last edited this post 13 years, 6 months ago.]
Hi BobRay,
Thank you very much for your answer!!
I'll try that solution!
BTW: I've read your book "MODX: The Official Guide" I really like it!
Thanks for buying my book.
That didn't work well.
While I don't know any PHP at all, I managed to modify it a bit, but it still isn't working to well.
When it is used in a snippet, it does work.
But when I put it in a Plugin it does even stall the manager.
Any suggestions?
<?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;
}
$this->rrmdir($dir . "/" . $object);
} else {
$prefix = substr($object, 0, 4);
if ( $prefix != '.git' && $prefix != '.svn') {
@unlink($dir . "/" . $object);
}
}
}
}
reset($objects);
$success = @rmdir($dir);
}
}
$id = $modx->resource->get('id');
$path = 'assets/content/hovenier/images/resource/' . $id; // replace with actual path to directory
rrmdir($path);
That didn't work well.
While I don't know any PHP at all, I managed to modify it a bit, but it still isn't working to well.
When it is used in a snippet, it does work.
But when I put it in a Plugin it does even stall the manager.
Any suggestions?
<?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;
}
$this->rrmdir($dir . "/" . $object);
} else {
$prefix = substr($object, 0, 4);
if ( $prefix != '.git' && $prefix != '.svn') {
@unlink($dir . "/" . $object);
}
}
}
}
reset($objects);
$success = @rmdir($dir);
}
}
$id = $modx->resource->get('id');
$path = 'assets/content/hovenier/images/resource/' . $id; // replace with actual path to directory
rrmdir($path);
Sorry, take out the '$this->'. I copied it from a class file and missed that one.
$this->rrmdir($dir . "/" . $object);
should be
rrmdir($dir . "/" . $object);
I'm sorry to bother you again BobRay, but I really can't solve this on my own.
I'm looking in to this for over an hour again now, and it is driving me crazy :S
Your code has two times $path, this doesn't seem right to me, is it?
$path = 'path/to/parent/directory/'; // replace with actual path to directory
$path = $id;
Sorry, that's another typo. The second one should be: