Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6593719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:43:06+00:00 2026-05-25T17:43:06+00:00

Is there anyway to get the current URI of a file in PHP, regardless

  • 0

Is there anyway to get the current URI of a file in PHP, regardless of which file it is being included?

for example:

a.php is included in b.php

But I want to get the URI of the directory in which a.php is located in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:43:06+00:00Added an answer on May 25, 2026 at 5:43 pm

    Current URI can normally be composed from $_SERVER['HTTPS'], $_SERVER['SERVER_NAME'] and $_SERVER['REQUEST_URI'] (except the hash identifier, #foo, which is not even sent to the server).

    You can find further reference here but it’s normally easier to print_r() the $_SERVER array.

    Clarification: a file can have one URL, many or none at all. There isn’t necessarily a 1 to 1 mapping between files and URLs.


    If we have this file layout:

    • /home/foo/htdocs/foo.php

        <?php
        include('./lib/utils.php');
        blah();
      
    • /home/foo/htdocs/lib/utils.php

        <?php
        function blah(){
        }
      

    … where /home/foo/htdocs is the document root, the way to to obtain the URL of the directory where utils.php lies is to read __FILE__ from within utils.php and replace leading $_SERVER['DOCUMENT_ROOT'] with the site’s protocol and path. E.g.:

    <?php
    
    if( isset($_SERVER['DOCUMENT_ROOT']) ){
        $directory = realpath(dirname(__FILE__));
        $document_root = realpath($_SERVER['DOCUMENT_ROOT']);
        $base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' .
            $_SERVER['SERVER_NAME'];
    
        if( strpos($directory, $document_root)===0 ){
            echo $base_url . str_replace(DIRECTORY_SEPARATOR, '/', substr($directory, strlen($document_root)));
        }
    }
    

    This code may need some tweaking (not all web servers create the same variables) and of course there’s no guarantee that the directory’s URL will be reachable or useful.

    Nota: an earlier version of this answer mentioned $_SERVER['HTTP_HOST']. While it’s fine most of the time, it won’t include the port number when a non-default port is being used. $_SERVER['SERVER_NAME'] does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there anyway to get the actual images used in resources? e.g., I want
I was wondering, is there anyway to get the current time of a CalendarEntry,
Is there anyway to get current publishing page with SharePoint API.
I have an infopath form, is there anyway to get this current infopath form
Is there anyway to get current signal strength on Android device. I know there
Hi is there anyway using Javascript to get user's current location and autocomplete fields
Is there anyway in GTK to get all of the current windows open by
is there anyway to get the ID of the current record I am INSERTING
Is there any way for me to get at a UITextField's current caret position
Is there anyway to get the Facebook ID of a user without forcing them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.