[英]Include path in PHP
here is my folders and files: 这是我的文件夹和文件:
Folder Main folder a - fila a.php folder b - file b.xml folder c - file c.php index.php
when I try to do this its shows error any suggestion or fixes. 当我尝试执行此操作时,显示错误或任何建议。
file c.php 文件c.php
<?php
class Abc{
public function __construct(){
$xml = simplexml_load_file('b/b.xml');
}}?>
so I include file c.php in a.php, which going to show error 所以我在a.php中包含文件c.php,这将显示错误
<?php require '../c/c.php'; $abc = new Abc(); ?>
file index.php 文件index.php
<?php
require 'c/c.php';
$abc = new Abc();
?>`
but if I include in index.php no prob. 但是,如果我将它包含在index.php中,则没有问题。 How do I fix the path ('b/b.xml') in c.php so that I can include it anywhere in the project. 如何在c.php中修复路径('b / b.xml'),以便可以将其包含在项目中的任何位置。
I found the way to fix it. 我找到了解决方法。 by doing this 通过做这个
file c.php 文件c.php
$xml = simplexml_load_file(dirname(dirname(__FILE__)).'/b/b.xml');
file a.php 文件a.php
include dirname(dirname(__FILE__)).'/c/c.php';
$abc = new Abc();
in file index.php 在文件index.php中
require dirname(__FILE__).'/c/c.php';
$abc = new Abc();
You can modify the c.php as follows: 您可以按如下方式修改c.php:
<?php
class Abc
{
public function __construct()
{
$xml = simplexml_load_file('mainfoldername/b/b.xml');
}
}
?>
This is the simple idea use in Joomla / Wordpress / Cakephp & other open source. 这是在Joomla / Wordpress / Cakephp和其他开放源代码中使用的简单思想。
In your index.php, use: 在您的index.php中,使用:
require( dirname( __ FILE__ ).'/c/c.php'); require(dirname(__ FILE__)。'/ c / c.php');
and in class construction, use: 在课堂上使用:
$xml = simplexml_load_file(dirname( __ FILE__ ) .'/b/b.xml'); $ xml = simplexml_load_file(dirname(__ FILE__)。'/ b / b.xml');
Let me know if this works. 让我知道这个是否奏效。
Thanks, Munjal 谢谢,Munjal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.