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

  • Home
  • SEARCH
  • 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 486003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:27:33+00:00 2026-05-13T01:27:33+00:00

In PHP, what would be the cleanest way to get the parent directory of

  • 0

In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:

$_SERVER['SCRIPT_NAME'] == '/relative/path/to/script/index.php'

Or just:

$something_else == '/relative/path/to/script/'

And I need to get /relative/path/to/ with slashes properly inserted. What would you suggest? A one liner is preferred.

EDIT

I need to get a path relative to the www root, dirname(__FILE__) gives me an absolute path in the filesystem so that won’t work. $_SERVER['SCRIPT_NAME'] on the other hand ‘starts’ at the www root.

  • 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-13T01:27:33+00:00Added an answer on May 13, 2026 at 1:27 am

    If your script is located in /var/www/dir/index.php then the following would return:

    dirname(__FILE__); // /var/www/dir
    

    or

    dirname( dirname(__FILE__) ); // /var/www
    

    Edit

    This is a technique used in many frameworks to determine relative paths from the app_root.

    File structure:

     /var/
          www/
              index.php
              subdir/
                     library.php
    

    index.php is my dispatcher/boostrap file that all requests are routed to:

    define(ROOT_PATH, dirname(__FILE__) ); // /var/www
    

    library.php is some file located an extra directory down and I need to determine the path relative to the app root (/var/www/).

    $path_current = dirname( __FILE__ ); // /var/www/subdir
    $path_relative = str_replace(ROOT_PATH, '', $path_current); // /subdir
    

    There’s probably a better way to calculate the relative path then str_replace() but you get the idea.

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

Sidebar

Related Questions

I'm programming in PHP and would like to create web pages which have email
Is anyone aware of a script/class (preferably in PHP) that would parse a given
I have a string encrypted in PHP that I would like to decrypt in
I was thinking of writing a PHP script that would analyse a CMS'd page's
We have a PHP project that we would like to version control. Right now
What are some reasons why PHP would force errors to show, no matter what
I'm not the best at PHP and would be extremely grateful if somebody could
Why would I want to use PHP's filter library? Why wouldn't I? It seems
I would like to use my laptop as a web development (PHP, Python, etc.)
I would like to call C# service with using PHP, anyone know how to

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.