.htaccess - How can we include php files without specifying the subfolder path
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?
Answer
Solution:
You can use
auto_prepend_file
directive in your .htaccess to make a.php
file included before all of your php files.If you're using
mod_php
then have this line in your .htaccess:Note that for
PHP-FPM
you need to have these equivalent line in your.user.ini
file:Then create a new file
env.php
in your project base directory with just one line: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:Answer
Solution:
You have to use function
set_include_path()
and somewhere in yourbootstrap.php
orindex.php
add next code:now everywhere below you can write this:
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.Answer
Solution:
Ok, I'm not a fan of doing this, but it will get the job done, as requested:
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:
to
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.
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.Answer
Solution:
This is how I would do it.
Then when you change it you just update
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.
Answer
Answer
Solution:
Here's an example of an approach that requires your files upfront via the auto_prepend_file directive.
bootstrap.php
:Add to php config, here .htaccess example:
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.
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.ini
auto_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 using
getcwd()
instead of$baseDir
works?: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 be
index.php
or a script included byindex.php
:Then your other scripts can call include using the constant:
Although if they all want to include the
config.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:
The other scripts might call this function like so:
Then when things change, you simply update one function in one file.
This all assumes you have an
index.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.