We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Can you post a few lines of the CSV file?

      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
      • 51020
      • 670 Posts
      Quote from: BobRay at Nov 19, 2017, 03:38 PM
      Can you post a few lines of the CSV file?


      There's not a lot to show - it's just this:
      "tag","resource"
      412,3126
      412,3127

      • discuss.answer
        • 3749
        • 24,544 Posts
        I assume the second number is the Resource ID. Give this a try:


        $path = 'full/path/to/csv/file';
        $fp = fopen($path, 'r');
        
        if (!$fp) {
           return "Could not open File " . $path . ' for reading';
        }
        
        $i = 0;
        while (($line = fgets($fp)) !== false) {
            /* Skip first line */
            if ($i == 0) {
                continue;
            }
        
            $i++;
            $parts = explode(',', $line);
            $docId = (int) $parts[1];
            $doc = $modx->getObject('modResource', $docId);
            if ($doc) {
                $doc->set('deleted', true);
                $doc->save();
            }
        }
        
        fclose($fp);
        
        return 'Marked ' . $i . ' resources for deletion.'; 
          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
          • 51020
          • 670 Posts
          Quote from: BobRay at Nov 20, 2017, 04:55 PM
          I assume the second number is the Resource ID. Give this a try:


          $path = 'full/path/to/csv/file';
          $fp = fopen($path, 'r');
          
          if (!$fp) {
             return "Could not open File " . $path . ' for reading';
          }
          
          $i = 0;
          while (($line = fgets($fp)) !== false) {
              /* Skip first line */
              if ($i == 0) {
                  continue;
              }
          
              $i++;
              $parts = explode(',', $line);
              $docId = (int) $parts[1];
              $doc = $modx->getObject('modResource', $docId);
              if ($doc) {
                  $doc->set('deleted', true);
                  $doc->save();
              }
          }
          
          fclose($fp);
          
          return 'Marked ' . $i . ' resources for deletion.'; 

          Hi Bob,

          I tried this, but i Just get the error:
          Could not open File full-path-to-file/filename.csv for reading

          (I did change the path to the actual URL of the file though - just changing here for security!)

          The permissions of the CSV file are set to 666

          Any ideas?
            • 17301
            • 932 Posts
            Do you have allow url fopen enabled in your php settings?
              ■ email: [email protected] | ■ website: https://alienbuild.uk

              The greatest compliment you can give back to us, is to spend a few seconds leaving a rating at our trustpilot: https://uk.trustpilot.com/review/alienbuild.uk about the service we provided. We always drop mention of services offered by businesses we've worked with in the past to those of interest.
              • 51020
              • 670 Posts
              Quote from: lkfranklin at Dec 04, 2017, 09:01 AM
              Do you have allow url fopen enabled in your php settings?

              I'm not sure - I will check.

              Thanks
              A
                • 3749
                • 24,544 Posts
                Remember that the code expects a physical path, not a URL. That way it won't need URL fopen.

                You might try changing the CSV file's permissions to 644.
                  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
                  • 51020
                  • 670 Posts
                  Quote from: BobRay at Dec 04, 2017, 12:39 PM
                  Remember that the code expects a physical path, not a URL. That way it won't need URL fopen.

                  You might try changing the CSV file's permissions to 644.

                  Ah that worked - thanks Bob! Very useful snippet I will save for future use!
                    • 3749
                    • 24,544 Posts
                    I'm glad we finally got it sorted. wink
                      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