Why simplexml_load_file does not work relative to host file?

穿精又带淫゛_ 提交于 2019-12-10 17:47:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!