-1

My structure is as follows

public_html/includes/ contains

includeMe.php

the php inside includeMe.php is

<?php echo basename(__DIR__); ?>

public_html/stuff/ contains

myPage.php

the php inside myPage.php is

<?php include '../includes/includeMe.php'; ?>

The output with this code when I visit stuff/myPage.php is

includes

when I want the output to be

stuff

since this is the parent of where its being included from.

I know this is the correct behaviour for the code i've used so my question is, what can I echo within includeMe.php so that the result when called from the include in myPage.php is stuff without the need to move the echo to the myPage.php itself?

Glen Keybit
  • 296
  • 2
  • 15
  • 1
    It looks like this has been already asked and answered: https://stackoverflow.com/questions/23105966/php-get-file-directory-of-an-included-file NOTE: if you're visiting url domain/stuff/page.php, you can extract "stuff" from url -> $_SERVER['REQUEST_URI']. Maybe that can be helpful in your case – Bozidar Sikanjic Dec 25 '22 at 10:24
  • @BozidarSikanjic I have had a read over and tried the answers in the question you linked to in my includeMe.php but the result is still always includes, not stuff :-/ – Glen Keybit Dec 25 '22 at 10:39

1 Answers1

-1

I tried a few things that worked, even with URL parameters:

//includeMe.php

echo $_SERVER['REQUEST_URI'];                    // /stuff/myPage.php?test=test

echo basename(__DIR__);                          // includes
echo basename(dirname($_SERVER['REQUEST_URI'])); // stuff
echo basename(dirname($_SERVER['SCRIPT_NAME'])); // stuff
echo basename(dirname($_SERVER['PHP_SELF']));    // stuff

I think it is noteworthy that this will only work with web request, not on CLI.

Skip
  • 95
  • 8