Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we include php files without specifying the subfolder path

Tags:

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 the include_once $_SERVER['DOCUMENT_ROOT'].'/functions/includes.php'; from the index.php and also included this includes.php file from all php files like header.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 use RewriteBase /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?

like image 827
Azzo Avatar asked Dec 13 '17 20:12

Azzo


2 Answers

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:

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

Note that for PHP-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 file env.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'; 
like image 77
anubhava Avatar answered Oct 20 '22 00:10

anubhava


You have to use function set_include_path() and somewhere in your bootstrap.php or index.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.

like image 40
cn007b Avatar answered Oct 19 '22 23:10

cn007b