We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16217
    • 4 Posts
    I have two variables that are directories on my site:

    $var1 = 'assets/images/imagefolder1';
    $var2 = 'assets/images/imagefolder2';


    These variables are in an include file. When I run the page, I get an error. What is the right way to write out the folder path. Do I need to use base_path somehow? Does it go outside the single quotes, or do I use
    $modx->config['base_path']
    in some way?

    Thanks in advance for your help!
      • 22303 MODX Staff
      • 10,725 Posts
      This really depends on how you are planning to use the variables. If you are going to work with files in those directories, MODX_BASE_PATH or $modx->config[’base_path’] with get you the full directory path to the folder on the server, i.e. $modx->config[’base_path’] . ’assets/images/foo/’. However, if you are going to use this in a URL, you’ll want to use $modx->config[’base_url’] (or $modx->config[’site_url’] if you want to include the complete URL including protocol, hostname, and base_url).
        • 16217
        • 4 Posts
        Yes, I’ll be using the files in those folders. So would the proper syntax be:

        $var1 = $modx->config[’base_path’].’assets/images/imagefolder1’;

        Just wanted to see if I had every jot and tittle in the right place. Thanks so much for your help.

          • 27376
          • 576 Posts
          If you’re intent is to include a list of files, I use a handy little piece of code that looks like this:
          <?php
          $files = array('assets/snippets/blah/file1.php', 'assets/snippets/other/file2.php');
          foreach ($files as $file) {
          	$path = $modx->config['base_path'].$file;
          	if (file_exists($path)) {
          		include($path);
          	} else {
          		die('file not found');
          	}
          }
          You’ll want to improve the error handling of course, probably by using [tt]$modx->logEvent()[/tt]. This is really for lists of files who’s count is unknown though, so it’s really up to you to decide is this an optimal solution.

          Enjoy!