We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    That may well be due to permissions problems. If your script is running as the webserver user, and you uploaded the file as your FTP user, then the script may not have permission to delete the file.
      Studying MODX in the desert - http://sottwell.com
      Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
      Join the Slack Community - http://modx.org
      • 3749
      • 24,544 Posts
      That suggests that $deleteids is empty or not an array. Check the spelling -- it’s case-sensitive ($deleteIds ?).

      In order to see output in a plugin, you need to terminate it with exit() or die(). They’re equivalent, but I find die() easier to type.

      echo $something;
      die();
      
      // or
      
      die ($something); 


      Try this after you have set $deleteids:

      die('Deleteids: ' . implode(', ',$deleteids) );


      Try this inside the loop:

      die("File: " . $value);


      It should print the path of the first file to delete.
        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
        • 10165
        • 129 Posts
        thanks BobRay

        I’ve tried adding the various die statements but it still just silently goes through the process and deletes the resource.. weird!

        the php log shows: PHP Parse error: syntax error, unexpected T_STRING

        here’s the code with some die statements...

        //Set the associated template variable ids that need to be deleted
        $tvID1 = "26";
        global $modx;
        $deletedids = $modx->event->params['ids'];
        die('deletedids: ' . implode(', ',$deletedids) );
        foreach ($deletedids as $key=>$value) {
        
            //Get the filepath of the tv1 (tvID1)
            $result = $modx->db->query( 'SELECT id, value FROM `modx_site_tmplvar_contentvalues` WHERE contentid = \'' .$modx->db->escape($value). '\' AND tmplvarid = \'' .$modx->db->escape($tvID1). '\' ORDER BY id ASC' );
            while( $row = $modx->db->getRow( $result ) ) {
        	$output = $row['value'];
        die("File: " . $value);
            }
        check that the file is not open already and then delete it
        $fh = fopen($output, 'w') or die("can't open file");
        fclose($fh);
        echo $output;
        die();
        unlink($output);
        }
        return;
        


          • 10165
          • 129 Posts
          Susan, thanks for the suggestion on ftp permissions, I’m currently testing this on local server MAMP (mac) and just to make sure I changed the permissions of the actual file to everyone but worth a try so thanks..
            • 3749
            • 24,544 Posts
            That error almost always means a syntax error, usually a bad comment or quoted item. If there’s any syntax error in a plugin, it simply fails to execute.

            This needs to be commented out:

            check that the file is not open already and then delete it
              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
              • 10165
              • 129 Posts
              thanks BobRay really appreciate your help on this - sorry I should have spotted that one!..

              I’ve now got : "deletedids: 115"

              if I comment this first die out then it still does nothing I was expecting the second die to give me something so not sure why it’s not
                • 3749
                • 24,544 Posts
                $deleteids may not be an array if there’s only one, in which case the foreach() would choke.

                Try this:

                $deletedids = (array) $modx->event->params['ids'];
                  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
                  • 10165
                  • 129 Posts
                  that’s a good point, I am only deleting (purging) one at a time...

                  I’ve altered this - I’m still not getting an echo beyond the first deletedids one (when commented out)

                  if I uncomment the open file and close it then I get "cant open file" but no echo messages..?
                    • 3749
                    • 24,544 Posts
                    That means that the code inside this loop is never executing, so $row is probably not valid because the query has failed.

                     while( $row = $modx->db->getRow( $result ) ) {
                    	$output = $row['value'];
                    die("File: " . $value);
                        }
                      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
                      • 10165
                      • 129 Posts
                      ok I’ve gone back to basics and got it working in a snippet...

                      then gone back to the plugin cut down :
                      //Set the associated template variable ids that need to be deleted
                      $tvID1 = "26";
                      global $modx;
                      $deletedids = (array) $modx->event->params['ids'];
                      foreach ($deletedids as $key=>$value) {
                      	
                          //Get the filepath of the tv1 (tvID1)
                          $result = $modx->db->query('SELECT id, value FROM `modx_site_tmplvar_contentvalues` WHERE contentid = \'' .$modx->db->escape($value). '\' AND tmplvarid = \'' .$modx->db->escape($tvID1). '\'');
                          while($row = $modx->db->getRow($result)) {
                      	$output = $row['value'];
                      unlink($output);
                      }
                      return;
                      


                      this is now giving a php error:
                      syntax error, unexpected $end