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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:26:02+00:00 2026-05-10T23:26:02+00:00

I’m in the process of setting up a php project, but am not very

  • 0

I’m in the process of setting up a php project, but am not very familiar with how to properly use php’s include/require commands. My layout currently looks like this:

/public   --apache points into this directory   /public/index.php   /public/blah/page.php   /utils/util1.php       -- useful classes/code are stored in other directories out here   /dbaccess/db1.php    dbaccess/db1.php      require '../utils/util1.php 

public/index.php

require '../dbaccess/db1.php' 

public/blah/page.php

require '../../dbaccess/db1.php' 

The problem is this from the php ‘include’ documentation:

If filename begins with ./ or ../, it is looked only in the current working directory

So public/blah/page.php fails because it includes dbaccess/db1.php which blows up when it tries to include util1.php. It fails because it’s relative path is from the original script in public/blah/, not from dbaccess/

This seems pretty stupid — db1.php has to just know where it’s being included from which isn’t going to work.

I’ve seen strategies like this:

require_once dirname(__FILE__) . '/../utils/util1.php'); 

That apparently works since now the path is an absolute path, but just seems really bizarre to me.

Is that normal? Should I continue down that path or am I missing something obvious here?

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T23:26:03+00:00Added an answer on May 10, 2026 at 11:26 pm

    Usually, the standard conventions are thus: like @grepsedawk said, you’ll want to define a constant that contains the root of your project folder and if you can the root of your includes folder:

    define('APP_ROOT', dirname(__FILE__)); define('INCLUDE_ROOT', APP_ROOT . '/includes'); 

    Note: the constant name needs to be a string!

    Also, you’ll notice I’m using dirname(__FILE__);. If you place your constants definition file in a subdirectory, you can do a dirname(dirname(__FILE__));, which is the equivalent of a ../.

    Now some other caveats. While PATH_SEPARATOR is a cool constant, it is not needed. Windows accepts / or \ in path names, and since Linux only users / as a path separator, go ahead and always use a / instead of mucking up your code with repeated references to PATH_SEPARATOR.

    Now that you have your root constants defined, what you’ll do when you need a configuration file included is a simple:

    include INCLUDE_ROOT . '/path/to/some/file.php'; 

    You’ll probably want your constant definitions (the define(...)‘s above) in a bootstrap script in your root directory:

    www_root/   index.php   bootstrap.php 

    The bootstrap will contain the defines (or an include of the constants file), as well as an include of any files that will be required by EVERY page.

    And finally the last standard convention you may not use, but if you start doing object oriented programming, the most common method (the PEAR standard) is to name your classes by using an _ to separate namespaces:

    class GlobalNamespace_Namespace_Class //... 

    And then organizing your file structure mapping name spaces to subdirectories (literally replacing all _’s with /’s):

    include_dir/   GlobalNamespace/       Namespace/           Class.php 

    And using __autoload() functions to load your classes, but that’s another question.

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.