I'm trying to use file_get_contents to reference a .json file in my module.

My directory structure is as follows

my custom field which is trying to reference the json file


my json file


If I use

    $json = 'modules/mod_kou_stack_exchange/media/js/sites.json';
    $json = file_get_contents($json);

PHP cannot find the file. I think perhaps because it is trying to reference it from the original file location. If I put the full location in the file it works, so long as I add an exception in Admin Tools. I don't want to hard code the URL as it might be used on other sites.

So then I have the option to use the URI function in Joomla.


However, if I try to use

use Joomla\CMS\Uri\Uri;
$uri = Uri::getInstance();
$url = $uri->toString();

within my code, it is then not accessible within the class that I am using

class JFormFieldStackexchangesites extends JFormFieldList

If I try to add use $uri and $url within the class that doesn't work either.

I'm sure this must be something to do with scope but having tried a few different ways I can't understand how to get it working.

Can anyone help me to understand how to get this working? And can anyone help me to understand why a relative path doesn't work?

  • 1
    Is the JSON file in ROOT/media/... or ROOT/modules/mod_kou_stack_exchange/media/...?
    – Lodder
    Commented Jun 14, 2020 at 14:51
  • Actually both lol. :) Let's use media :)
    – Eoin
    Commented Jun 14, 2020 at 16:28

2 Answers 2


Don't use JUri for fetching local files. It returns the web address of your site. Fetching files over HTTP is extremely slow compared to fetching files locally. Use one of JPATH constants to get a local filesystem path, e.g.:

$json = JPATH_BASE . '/modules/mod_kou_stack_exchange/media/js/sites.json';
$json = file_get_contents($json);


  • Is it just PATH_BASE under the new naming conventions / Namespacing?
    – Eoin
    Commented Jun 16, 2020 at 12:46
  • 1
    No, it's a global constant. It exists since at least 1.5. There is no PATH_BASE constant in Joomla.
    – Sharky
    Commented Jun 17, 2020 at 10:08

Use the filepath:

JUri::base() . '/modules/mod_kou_stack_exchange/media/js/sites.json'

The filesystem equivalent of, eg. /public_html/yoursite/modules/... or /htdocs/yoursite/modules/... where Joomla is installed in yoursite directory.

  • 1
    But we don't need to use JUri any more right? We can just use Uri::base(). Now I see the problem, it is because I am in the administrator, so Uri::base() is in fact with /administrator on the end. Ah ha! I can use Uri::root()
    – Eoin
    Commented Jun 14, 2020 at 16:33
  • 1
    Technically yes, JURI has been deprecated, but as no new features are being pushed to 3.X, it won't stop working in Joomla 4 either, it'll likely be removed completely in Joomla 5.
    – Grant G
    Commented Jun 14, 2020 at 20:38
  • Even so I'd rather do the most up-to-date way for the sake of learning best practice :)
    – Eoin
    Commented Jun 16, 2020 at 12:46

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.