.htaccess - How can we include php files without specifying the subfolder path

579

Normally we use the following code to include php files inside each other:

<?php 

include_once 'include/config.php';
// OR
include 'include/config.php'; 
// OR 
include_once $_SERVER['DOCUMENT_ROOT'].'include/config.php';
// ect...
?>

But the above codes only apply if the php files are in the root file. I mean, if we move our files into a subfolder. We need to make a change to the code we included in the php files. For example like this:

<?php 
    include_once 'subfolder/include/config.php';
    // OR
    include 'subfolder/include/config.php'; 
    // OR 
    include_once $_SERVER['DOCUMENT_ROOT'].'/subfolder/include/config.php';
    // ect...
?>

What I'm saying is that when we move our php files into the subfolder, then include_once want to see subfolder name like (include_once 'subfolder/include/config.php';). This is a challenging situation because we need to do this for the included page in many files.

For example i include theinclude_once $_SERVER['DOCUMENT_ROOT'].'/functions/includes.php'; from theindex.php and also included this includes.php file from all php files likeheader.php, posts.php, and ajax_post.php . It is working fine from the root folder but if we move the files in the subfolder then include.php file not including without subfolder name.

Maybe it is also possible to do this with.htaccess.

I have made this htaccess code maybe you have a solution with htaccess. I must be say i have tryed to useRewriteBase /subfoldername/ but include files didn't worked.

Options +FollowSymLinks -MultiViews
RewriteEngine On 
RewriteBase /

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^group/([\w-]+)/?$ sources/group.php?group_username=$1 [L,QSA]  
RewriteRule ^profile/([\w-]+)/?$ sources/user_profile.php?username=$1 [L,QSA]
RewriteRule ^profile/(followers|friends|photos|videos|locations|musics)/([\w-]+)/?$ sources/$1.php?username=$2 [L,QSA]     

RewriteRule ^admin/(.*)$ admin/index.php?page=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]  

RewriteRule ^(.+?)/?$ index.php?pages=$1 [L,QSA]

. My responsibility is, how can we include php files without subfolder name?

166

Answer

Solution:

You can useauto_prepend_file directive in your .htaccess to make a.php file included before all of your php files.

If you're usingmod_php then have this line in your .htaccess:

php_value auto_prepend_file "/Applications/MAMP/htdocs/script/env.php"

Note that forPHP-FPM you need to have these equivalent line in your.user.ini file:

auto_prepend_file = "/Applications/MAMP/htdocs/script/env.php"

Then create a new fileenv.php in your project base directory with just one line:

<?php
   $baseDir = __dir__ . '/';
?>

This line sets a variable$baseDir that has value of your project base directory i.e./Applications/MAMP/htdocs/script/ OR/Applications/MAMP/htdocs/subdir/script/

Then use this variable$baseDir anywhere you have to include other files such as:

include_once $baseDir.'functions/includes.php';

include_once $baseDir.'functions/get.php';
193

Answer

Solution:

You have to use functionset_include_path() and somewhere in yourbootstrap.php orindex.php add next code:

<?php
$path = '/usr/pathToYourDir';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

now everywhere below you can write this:

include_once 'include/config.php';
// or
include_once 'include/db/index.php';
// etc

And in case you need to move your code into another directory - you have only to change value of path varibalbe$path = '/usr/pathToYourDir'; to new path to your directory.

654

Answer

Solution:

Ok, I'm not a fan of doing this, but it will get the job done, as requested:

$newpath="";

$dirs = array_filter(glob('*'), 'is_dir');
foreach ($dirs as $location) {
    $newpath .= PATH_SEPARATOR . $location; 
}
set_include_path(get_include_path() . $newpath);

The above code will find all subfolders from where this file is running, and add them all to the current include path. by changing the glob from:

glob('*') 

to

glob('includes/*')  or glob('includes' . DIRECTORY_SEPARATOR . '*')

You can restrict it to only subfolders under includes.

The above is not tested, I just threw the code together, but it illustrates a way to do what you're asking for.

I would still recommend putting files in determined locations rather than trying to include them from anywhere.

119

Answer

Solution:

__DIR__ is certainly what you are looking for. Use it to provide relative paths for required files. I would highly suggest usingrequire_once orinclude_once for all library files.

include_once dirname(dirname(__DIR__))."/include/config.php";
121

Answer

Solution:

This is how I would do it.

//----- some global location like index.php

if(!defined("CONFIG_PATH")) define("PATH_CONFIG", __DIR__."/include/");

//

Then when you change it you just update

{-code-2}

I would say this is the "Traditional" way to do it, and it's perfectly fine to use a global constant for this. It's not something you want changing at run time. You need access to it in many places, potentially. You can check if your files are accessed properly .. etc.

Many many many, MVC frameworks do it this exact way.

88

Answer

-- then later in some other file if(!defined("CONFIG_PATH"))die("Config path is not defined."); include_once CONFIG_PATH.'config.php';|||if(!defined("CONFIG_PATH")) define("PATH_CONFIG", __DIR__."/include/subfolder/");
642

Answer

Solution:

Here's an example of an approach that requires your files upfront via the auto_prepend_file directive.

bootstrap.php:

<?php

$require_upfront = function($dir) {
    require_once $dir . '/one.php';
    require_once $dir . '/two.php';
};

$require_upfront(__DIR__ . '/inc');

Add to php config, here .htaccess example:

php_value auto_prepend_file "/abs/path/to/bootstrap.php"
844

Answer

Solution:

If you have the file's name unique in all server, you can make a research of this using a research like this.

I think that this solution isn't better when this script run a lot of, but you can include if the include function fails, using try and catch (if you use this method watch the php version) or if you know that this path change once a day you can make a file which is launched with routine.

If you make this, you need to store this path, but I don't know if there is a method for make global variable. My solution is to store or in a DB or in a file.

I hope I have helped you.

371

Answer

Solution:

What I understand from the answer https://stackoverflow.com/a/50632691/7362396 is that you need to include files based on the directory of the entrypoint script.

A solution for this without the need for Apache or php.iniauto_prepend_file could be to usegetcwd(). It returns the current working directory, which should be the path of the entrypoint/main script, even when called in included files.

Can you try if simply usinggetcwd() instead of$baseDir works?:

include_once getcwd().'/functions/includes.php';

include_once getcwd().'/functions/get.php';
945

Answer

Solution:

Using Apache to fix your app is solving it at the wrong level.

If you have to update a bunch of files in your code when the location of a file changes, that is your code telling you that you have made a bad architectural decision. So fix the actual problem and stop repeating yourself.

Define where your includes live in one place, and then when needed, update that one place accordingly.

One way to do this is set a constant at the root of your app. This assumes you have a bit of code at the root of your app that is called on every page. This would likely beindex.php or a script included byindex.php:

// index.php or a file included by index.php
const MY_INCLUDE_PATH = __DIR__ . '/path/to/subfolder`

Then your other scripts can call include using the constant:

// some other script included to handle the page
include MY_INCLUDE_PATH . '/config.php';
include MY_INCLUDE_PATH . '/some-other-include.php';

Although if they all want to include theconfig.php file, maybe just go ahead and include it in yourindex.php.

This is not a "bad" global you want to avoid. It is using constants as they are intended. Constants are part of the namespace, if you are using a namespace for your app.

This assumes there is one include subfolder that sometimes changes places and a script somewhere that is a part of every request. Is that correct? If not, please post your directory structure.

A function would also work, and could include logic based on things such as the path of the calling script:

// index.php or a file included by index.php
my_include_function($path) {
  //do stuff
}

The other scripts might call this function like so:

// some other script included to handle the request
my_include_function(__FILE__);

Then when things change, you simply update one function in one file.

This all assumes you have anindex.php or similar script that handles every request and can be the single point of control for defining the function or constant. If that's not the case, it should be!

Updating your code to use a router (here is one) would take maybe 10 minutes and potentially simplify that gnarly .htaccess file you posted.

People are also looking for solutions to the problem: Adding php-curl in a Yocto build

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.