We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48706
    • 5 Posts
    I'm trying to write a a file retrieval snippet that outputs files in a non-web accessible folder. I was wondering if their was a way to change the content type in a snippet to prevent an xml, js, jpeg, etc from getting displayed incorrectly?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      How are you outputting the file? Are you creating static resources? You can specify the content type as part of the fields array, getting its value from the file extension while looping over the files (I would use the pathinfo($fullpath, PATHINFO_EXTENSION) function):

      switch ($extension) {
          case 'xml':
              $type = 2;
              break;
          case 'js':
              $type = ?;
              break;
      etc. etc.
      

      You can look at the content_type table in the database to see what the ID of the various types are.
      $response = $modx->runProcessor('resource/create',array(
         'pagetitle' => 'Page Title',
         'published' => true,
         'content' => 'path-to-file',
         'content-type' => $type,
         /* etc */
      ));
      if ($response->isError()) {
         if ($response->hasFieldErrors()) {
             $fieldErrors = $response->getAllErrors();
             $errorMessage = implode("\n",$fieldErrors);
         } else {
             $errorMessage = 'An error occurred: '.$response->getMessage();
         }
         return $errorMessage;
      }
      return 'Success!';
        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
        • 48706
        • 5 Posts
        wandering213 Reply #3, 12 years ago
        I don't want to create static resources (or at least I don't see the benefit for it yet), I just want to retrieve the files and output them as needed. Each set of resources is an independent 360 tour that I'm hosting through my website for clients.

        Essentially, I just want to serve the files in a way that protects them from being directly accessed (except through in authorized ways). Right now they are just in a obscure location that's web accessible, which bothers me that anyone could just download them if they looked at the page source code.

        So I might be way off base with my thinking, and open to alternative approached too.

        Thanks,
        Robert