We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20371
    • 58 Posts
    Hey,

    I have a feeling this is a dumb question because it seems like the answer should be fairly straight forward. Having said that, my google fu hasn't been able to turn up anything so here goes.

    I'm accessing a static resources and getting paths ala

    $page = $modx->getObject('modResource', $id);
    $fileUrl = $page->getSourceFile();
    


    So $fileUrl is the path to the static resource from the webroot.

    Now if I want to read that file in the snippet, I need either a path relative to cwd or an absolute path.

    So what's the best way to convert the path from the webroot, to a relative or absolute path ?

    Thanks in advance! [ed. note: Mr5o1 last edited this post 12 years, 7 months ago.]
      • 3749
      • 24,544 Posts
      First, it sounds like you want a path, not a URL, so the $fileUrl variable name is confusing.

      You might try this:

      $filePath = MODX_BASE_PATH . $filePath;
      
      // or if you really want a URL
      
      $fileUrl = MODX_BASE_URL . $filePatch;
      


      Those assume that the resource_static_path of the object is never empty. If it ever is empty, you may have to send something in the resource_static_path member of $options argument to getSourceFile(), like this:

      $options = array();
      $options['resource_static_path'] = 'some/path';
      $filePath = $page->getSourceFile($options);
      


        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
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        Static resources work with files, so it's the path you need. Static resources don't work the same way static elements do; the path is the resource content.
          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
          FYI, this is the code of modStaticResource->getSourceFile():

           /**
               * Get the absolute path to the static source file represented by this instance.
               *
               * @param array $options An array of options.
               * @return string The absolute path to the static source file.
               */
              public function getSourceFile(array $options = array()) {
                  if (empty($this->_sourceFile)) {
                      $filename = parent :: getContent($options);
                      if (!empty($filename)) {
                          $array = array();
           
                          if ($this->xpdo->getParser() && $this->xpdo->parser->collectElementTags($filename, $array)) {
                              $this->xpdo->parser->processElementTags('', $filename);
                          }
                      }
           
                      if (!file_exists($filename)) {
                          $this->_sourcePath= $this->xpdo->getOption('resource_static_path', $options, $this->xpdo->getOption('base_path'));
                          if ($this->xpdo->getParser() && $this->xpdo->parser->collectElementTags($this->_sourcePath, $array)) {
                              $this->xpdo->parser->processElementTags('', $this->_sourcePath);
                          }
                          $this->_sourceFile= $this->_sourcePath . $filename;
                      } else {
                          $this->_sourceFile= $filename;
                      }
                  }
                  return $this->_sourceFile;
              }
            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
            • 20371
            • 58 Posts
            Nice. Thanks so much for the help. I guess the MODX_BASE_PATH and MODX_BASE_URL were the constants I was looking for..

            @Bobray to answer your question re path vs url.. you're right, my example wasn't that clear.. sorry.

            What I'm actually doing is getting a url returned from phpthumbof ala /assets/images/foo.jpg and then trying to access that file via php. So yes, $modx->getObject()->getSourceFile() was certainly a bit misleading.. sorry!
              • 3749
              • 24,544 Posts
              Ah, I get it now. The first thing to try is something like this:

              $path = str_replace(MODX_BASE_URL, MODX_BASE_PATH, $url);


                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