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 7184827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:16:54+00:00 2026-05-28T18:16:54+00:00

Writing code in PHP, how can I ensure the require_once path of a script

  • 0

Writing code in PHP, how can I ensure the require_once path of a script is translated correctly regardless of where the calling script is?

Assuming a folder structure of:

ROOT
    {subfolder
    {subfolderX

In my “script A” which is in a subfolder called scripts it requires another script in another subfolder called “script B,” so my require looks like

require_once("../subfolderX/script B");

This all works okay. But if I call script A from another script which is say up a level such as:

require_once("subfolder/script A");

The call to script B doesn’t work becasue it’s now relative to the new calling script.

What is the correct technique here to ensure the path stays relative to the calling script?

  • 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-28T18:16:55+00:00Added an answer on May 28, 2026 at 6:16 pm

    Use the PHP magic constant __DIR__, which will point to the directory that the script resides in. In other words, do this:

    require_once __DIR__ . '/../subfolderX/script B';
    

    Instead of this:

    require_once '../subfolderX/script B';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When writing PHP code for any given project, do you find you can write
When writing PHP code, you can cause some really hard to track down parsing
I have found that writing PHP code within classes can become rather long: $this->parser->parse_syntax($this->get_language_path($this->language),
I am writing PHP code where I want to pass the session id myself
While writing code in a file that would comprise of PHP, HTML, CSS &
When writing code do you consciously program defensively to ensure high program quality and
i want to make my server side in php. after writing the code in
I've been writing some code for PHP 5.3, and I wanted to do something
I am writing some PHP code where I end it with a email validation
I am writing a code in PHP and line is as follows $xml_output .=

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.