为什么 simplexml_load_file 相对于主机文件不起作用? [英] Why simplexml_load_file does not work relative to host file?
问题描述
我在包含的 PHP 文件中放置了 simplexml_load_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?
推荐答案
您始终可以使用神奇的 __FILE__ 常量,因此您可以像这样编写 simplexml_load_file() 调用:
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');
更新
从 PHP 5.3 开始,引入了一个新的 __DIR__
,你可以用它代替 dirname(__FILE__)
像这样:
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');
这篇关于为什么 simplexml_load_file 相对于主机文件不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!