We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16430
    • 217 Posts
    Thats almost exactly what I need, except few changes...

    I have only two folders:

    assets/images1
    assets/images2

    And inside both folders there are images with name: xxx-image-some_name.jpg, where xxs is id of particular resource.

    On deleting resource I want to also delete all images from those two folders, that begins with the same id...
    How to modify this plugin? Thanks...
      • 16430
      • 217 Posts
      Solved using this plugin...
      <?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;
        • 37914
        • 89 Posts
        It took some time before I could go back on testing this script ;P
        It works great!
        Thanx BobRay!

        I modified the code a little after seeing kudykam his script:

        <?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);
          • 3749
          • 24,544 Posts
          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);
                  }
              }
          }
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 37914
            • 89 Posts
            Quote from: BobRay at Jan 28, 2015, 10:13 PM
            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);
                    }
                }
            }

            Thanx BobRay!
            I tested it and it works great with that foreach loop modified.
              • 3749
              • 24,544 Posts
              I thought it might. wink

              I'm glad it works for you.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting