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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:09:42+00:00 2026-06-13T12:09:42+00:00

In PHP when we include or require some file using some initializers like the

  • 0

In PHP when we include or require some file using some initializers like the following. How can we overcome filepath issues which occurs if you include the same initializer in a sub-directory or different location.

<?php

// settings
$settings = array('config');

foreach ($settings as $setting) {
    require_once "../system/settings/{$setting}.php";
}

// neutrals
$neutrals = array('functions');

foreach ($neutrals as $neutral) {
    require_once "../system/neutrals/{$neutral}.php";
}

// helpers
$helpers = array('database', 'file', 'logger', 'user', 'session', 'database');

foreach ($helpers as $helper) {
    require_once "../system/helpers/{$helper}.php";
}

// models
$models = array('test');

foreach ($models as $model) {
    require_once "../system/models/{$model}.php";
}

?>

Above script is in a file all_initializer.php. The hurdle here is that i cant use the same initializer in a public sub-directory or other location as it will occur fatal errors (if its a required file) of file not found.

EDIT

For e.g. I am using this initialzer in a public folder in a index.php file then there’s a sub-directory in the public folder public/sub. How can I use the same initializer in public/sub/index.php as I used in public/index.php ?

  • 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-06-13T12:09:43+00:00Added an answer on June 13, 2026 at 12:09 pm

    You could use the file_exists to avoid fatal errors

    // neutrals
    $neutrals = array('functions');
    
    foreach ($neutrals as $neutral) {
        if (file_exists("../system/neutrals/{$neutral}.php") 
        {
            require_once "../system/neutrals/{$neutral}.php";
        }
        else
        {
            // Do some logging here so as to know that something went wrong
        }
    }
    

    As for the path issues you are referring to, you can include this file from anywhere so long as you supply the proper base path for your operations. Defining a ROOT_PATH in your index.php file would help you detect where your script is and what it needs to load.

    So for instance if you have this structure:

    /system
    /system/neutrals
    /system/models
    /public
    /public/index.php
    

    in your index.php you can define a ROOT_PATH constant that will be used throughout the application and serve as a point of reference.

    // this points to the folder that has /public and /system
    define('ROOT_PATH', dirname(dirname(__FILE__)))); 
    

    You can also have a constant just for your system folder

    define('SYSTEM_PATH', ROOT_PATH . '/system'); 
    

    and then all your require_once declarations become:

    require_once SYSTEM_PATH ."/neutrals/{$neutral}.php";
    

    EDIT: Based on additional information in the question

    Structure:
    /all_includes.php
    /system
    /system/neutrals
    /system/models
    /public
    /public/index.php
    /public/sub/index_sub.php
    

    In index.php you define

    // this points to the folder that has /public and /system
    define('ROOT_PATH', dirname(dirname(__FILE__)))); 
    

    and then:

    require_once ROOT_PATH . '/all_includes.php';
    

    to do your initialization. The same thing happens in the public/sub/index_sub.php.

    Your all_includes.php becomes:

    // neutrals
    $neutrals = array('functions');
    
    foreach ($neutrals as $neutral) {
        if (file_exists(ROOT_PATH . "/system/neutrals/{$neutral}.php") 
        {
            require_once ROOT_PATH . "/system/neutrals/{$neutral}.php";
        }
        else
        {
            // Do some logging here so as to know that something went wrong
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

whenever we include\require some file using php like require ('links.php'); OR include ('links.php'); which
How can you use jQuery to include/require a different php file if window height
Is there any way to include php file using require and calling a php
I've got some functions in my file that require taxonomy.php to execute. I'm attempting
I'm trying to post some very simple data to a php file using jquery
For some years that I've been using my own PHP template engine, which is
I'm going to pass through some values between two required/included php file,ex: mysql.php: <?php
In my live site I have php include() and require() that have full path
I'm using require_once() in header.php to include the FirePHP library. In the page.php I'm
when I include vars.php: $var='hello'; in index.php: require 'vars.php'; echo $var; it works great,

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.