问题
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?
回答1:
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');
来源:https://stackoverflow.com/questions/11468919/why-simplexml-load-file-does-not-work-relative-to-host-file