0

I'm working on index.php file which located in /folder1/folder2/folder3/index.php and contains some php code as below:

<?php 
include $_SERVER['DOCUMENT_ROOT'].'/path/to/the/filename.php'; 
$root = new SimpleXMLElement($xmlstr); 
echo $root->subroot[0]->data->title; 
?>

The filename.php doesn't change name or extension. But, folders and subfolders that lead to filename.php carry different names depending on current circumstances.

For example, in an instance folders are named as /a1/a2/a3/filename.php and in another instance named as /b1/b2/b3/filename.php. Filename.php carries the same name in all instances but inside contains different data.

If that helps, the filename.php structured below:

<?php
$xmlstr = <<<'XML'
<?xml version='1.0' standalone='yes'?>
<root>
 <subroot>
  <data>
   <title>
    -- some text here --
   </title>
  </data>
 </subroot>
</root>
XML;
?>

Since folders and subfolders are already created and their names are known, I want (if that is possible) to code the /path/to/the/ so it won't be necessary for the user to type the corresponded path each time creates a new index.php file. I try something like this:

<?php 
include $_SERVER['DOCUMENT_ROOT'].['__DIR__']'filename.php'; 
$root = new SimpleXMLElement($xmlstr); 
echo $root->subroot[0]->data->title; 
?>

...and:

<?php 
include $_SERVER['DOCUMENT_ROOT'].['REQUEST_URI']'filename.php'; 
$root = new SimpleXMLElement($xmlstr); 
echo $root->subroot[0]->data->title; 
?>

...but both don't work. Any suggestions or ideas?

1 Answer 1

1

try using

 __DIR__ . DIRECTORY_SEPARATOR . 'filename.php'; 

http://php.net/manual/en/dir.constants.php http://php.net/manual/en/language.constants.predefined.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.