0

Suppose, we have a file: /sites/default/files/picture.jpg

Drupal is installed into a subdirectory: /drupal. There are 2 domains, attached to the site:

  1. exchange

  2. exchange-test (uses different DB)


 | domain        | settings.php path     |
 |---------------+-----------------------+
 | exchange      | /sites/default/       |
 | exchange-test | /sites/exchange-test/ |

When we call

print file_create_url('public://picture.jpg');

we get:

| site          | result                                        |
|---------------+-----------------------------------------------|
| exchange      | /drupal/sites/default/files/picture.jpg       |
| exchange-test | /drupal/sites/exchange-test/files/picture.jpg |

But the file is stored in default directory even for exchange-test domain.

As far as I could find, the path is being generated, using function conf_path. It scans the config directory to find a subdir with current domain name:

 $server = explode('.', implode('.', array_reverse(explode(':', rtrim($_SERVER['HTTP_HOST'], '.')))));
  for ($i = count($uri) - 1; $i > 0; $i--) {
    for ($j = count($server); $j > 0; $j--) {
      $dir = implode('.', array_slice($server, -$j)) . 
             implode('.', array_slice($uri, 0, $i));
      if (isset($sites[$dir]) && 
          file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $sites[$dir])) {
            $dir = $sites[$dir];
      }
      if (file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir . '/settings.php') || 
         (!$require_settings && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir))) {
            $conf = "$confdir/$dir";
            return $conf;
      }
    }
  }

If found, it uses this directory as files path.

So, one way is to temporarily change $_SERVER['HTTP_HOST']:

$_SERVER['HTTP_HOST'] = 'exchange';
print file_create_url('public://picture.jpg');
$_SERVER['HTTP_HOST'] = 'exchange-test';

Will give the correct result. But this trick seems clumsy to me. Is there any way to get the default directory without such hacks?

2
  • try setting the public file system path in your -test site to match that of the other in /admin/config/media/file-system
    – Geoff
    Commented Dec 23, 2014 at 22:57
  • @Geoff Can you put your comment as an answer, so I could close the question?
    – user4035
    Commented Dec 24, 2014 at 7:15

1 Answer 1

1

try setting the public file system path in your -test site to match that of the other in /admin/config/media/file-system

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.