We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37914
    • 89 Posts
    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?
      • 3749
      • 24,544 Posts
      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.]
        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
        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!
          • 3749
          • 24,544 Posts
          Thanks for buying my book. smiley
            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
            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);

              • 37914
              • 89 Posts
              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);

                • 3749
                • 24,544 Posts
                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);
                  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
                  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;
                    • 40045
                    • 534 Posts
                    Be sure to supply the full path to rmdir! Meaning

                    $modx->getOption('base_path') . '/path/to/directory/'
                      • 3749
                      • 24,544 Posts
                      Sorry, that's another typo. The second one should be:

                      $path .= $id;
                        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