We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10165
    • 129 Posts
    hey Bob, think we’re getting somewhere...

    just got this:

    string(52) "assets/files/Font Best Practices in Mac OS X.pdf"

    when I try use the unlink on this result it still won’t delete the file tho..?


    here’s the code up to date:
    <php
    $tvID1 = "26";
    global $modx;
    $deletedids = (array) $modx->event->params['ids'];
    //$deletedids = $modx->event->params['ids'];
    //var_dump($deletedids);
    foreach ($deletedids as $key=>$value) {
    
        $v = $modx->db->escape($value);
        $t = $modx->db->escape($tvID1);
        $res = $modx->db->select("value", $modx->getFullTableName('site_tmplvar_contentvalues'), 
        "contentid=$v AND tmplvarid=$t");
        if($modx->db->getRecordCount($res))
        {
           while($row = $modx->db->getRow($res))
           {
           $value = $row["value"];
           var_dump($value);
           //unlink($value);
           }
        } else {
        die('Query failed');
        exit;
        }
    }
    return;
    ?>
    

      • 10165
      • 129 Posts
      I’ve added the base_url to try the unlink but haven’t got it working yet..

             unlink($modx->config['base_url']$value);
      

        • 28042 ☆ A M B ☆
        • 24,524 Posts
               unlink($modx->config['base_url'].$value);

        don’t forget the . to concatenate the one variable value with the other.
          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
          • 10165
          • 129 Posts
          I’m still not getting the file to delete - I’ve added an if_exists(

          If I comment out the unlink and echo the $filepath it shows correctly /assets/files/........pdf

          but when I comment out the echo / die and try get it to delete the file, it comes up with the else.. No File Exists? huh

          $tvID1 = "26";
          global $modx;
          $deletedids = (array) $modx->event->params['ids'];
          //$deletedids = $modx->event->params['ids'];
          //var_dump($deletedids);
          foreach ($deletedids as $key=>$value) {
          
              $v = $modx->db->escape($value);
              $t = $modx->db->escape($tvID1);
              $res = $modx->db->select("value", $modx->getFullTableName('site_tmplvar_contentvalues'), 
              "contentid=$v AND tmplvarid=$t");
              if($modx->db->getRecordCount($res))
              {
                 while($row = $modx->db->getRow($res))
                   {
                   $value = $row["value"];
                   //var_dump($value);
                   //echo    $modx->config['base_url'].$value;
                    //die;
                   $filepath = $modx->config['base_url'].$value;
                              if(file_exists($filepath)) {
                              echo $filepath;
                              die;
                              //unlink($filepath);
                              } else {
                                die('No File Exists');
                              }
                     }
              } else {
              die('Query failed');
              exit;
              }
          }
          return;
          

            • 10165
            • 129 Posts
            Success, Eureka and similar expressions!...

            Finally after a long journey it works, here’s the final code:

            Thanks for all your help with this BobRay and Susan!! My php/sql knowledge has taken a (small) leap forward!
            MODx rules : )

            I intend to add around 7 tv file fields to be deleted so that should be fun..!

            (for anyone else that might find this useful you’ll need to change the file path to suit your server root)


            $tvID1 = "26";
            global $modx;
            $deletedids = (array) $modx->event->params['ids'];
            //$deletedids = $modx->event->params['ids'];
            //var_dump($deletedids);
            foreach ($deletedids as $key=>$value) {
            
                $v = $modx->db->escape($value);
                $t = $modx->db->escape($tvID1);
                $res = $modx->db->select("value", $modx->getFullTableName('site_tmplvar_contentvalues'), 
                "contentid=$v AND tmplvarid=$t");
                if($modx->db->getRecordCount($res))
                {
                   while($row = $modx->db->getRow($res))
                     {
                     $value = $row["value"];
                     //var_dump($value);
                     //echo    $modx->config['base_url'].$value;
                      //die;
                     $filepath = "/Applications/MAMP/htdocs/".$value;
                                if(file_exists($filepath)) {
                                //echo $filepath;
                                //die;
                                @unlink($filepath);
                                } else {
                                 die("$filepath DOES NOT EXIST");
                                }
                       }
                } else {
                die('Query failed');
                exit;
                }
            }
            return;
            

              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Congratulations! grin grin
                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
                I’m glad you got it working. smiley

                Thanks for contributing the final version.
                  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