Search code examples
phpsimplexmlrelative-path

Why simplexml_load_file does not work relative to host file?


I have simplexml_load_file instruction placed withing included PHP file. But this instruction works depending on where I include this file from. Why? Is it possible to make relative file path be interpreted relatively to the file instruction placed in?


Solution

  • You can always access the current file's full path with the magic __FILE__ constant, so you can write your simplexml_load_file() call like this:

    <?php
    simplexml_load_file(dirname(__FILE__).'/file.xml');
    

    Update

    Since PHP 5.3, there have been a new __DIR__ introduced, you can use it in place of the dirname(__FILE__) like this:

    <?php
    simplexml_load_file(__DIR__).'/file.xml');