How to include php files without specifying a subfolder path

Usually we use the following code to include php files in 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 to a subfolder. We need to make changes to the code that we included in the php files. For example, for example:

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

I say that when we move our php files to a subfolder, then include_once wants to see the name of the subfolder, for example ( include_once 'subfolder/include/config.php'; ). This is a difficult situation because we need to do this for the included page in many files.

For example, I include include_once $_SERVER['DOCUMENT_ROOT'].'/functions/includes.php'; from index.php and also included this file header.php, posts.php, and ajax_post.php from all php files such as header.php, posts.php, and ajax_post.php . It works fine in the root folder, but if we move the files in a subfolder, then the include.php file is not included without the name of the subfolder.

Perhaps this is also possible with .htaccess .

I made this htaccess code, maybe you have a solution with htaccess. I must say that I tried to use RewriteBase/subfoldername/ but the files could not be included.

 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 to include php files without a subfolder name?

+23
source share
10 answers

You can use the auto_prepend_file directive in your .htaccess to make the .php file included in front of all your php files.

If you use mod_php , enter 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 an equivalent line in the .user.ini file:

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

Then create a new env.php file in the project’s base directory with only one line:

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

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

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

 include_once $baseDir.'functions/includes.php'; include_once $baseDir.'functions/get.php'; 
+15
source

__DIR__ is definitely what you are looking for. Use it to provide relative paths for required files. I would suggest using require_once or include_once for all library files.

 include_once dirname(dirname(__DIR__))."/include/config.php"; 
+6
source

You should use the set_include_path() function and add the following code somewhere in your bootstrap.php or index.php :

 <?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 to another directory - you only need to change the path value varibalbe $path = '/usr/pathToYourDir'; to a new path to your directory.

+6
source

Well, I'm not a fan of this, but he will do the job 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 the subfolders from which this file is running, and add them to the current include path. by changing glob from:

 glob('*') 

to

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

You can limit it to only subfolders under included.

The above is not verified, I just added the code together, but it illustrates a way to do what you ask for.

I still recommend placing files in specific places, rather than trying to include them from anywhere.

+5
source

This is how I do it.

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

Then when you change it, you just update

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

I would say that this is a “traditional” way to do this, and it’s great to use a global constant for this. This is not what you want to change at runtime. You may need access to it in many places. You can check if your files are available correctly .. etc.

Many many MVC frameworks do just that.

+4
source
+3
source

Here is an example approach that requires your files using 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" 
+2
source

If you have a file name unique to the entire server, you can research this using research, for example.

I think this solution is not better when this script runs a lot, but you can enable it if the include function doesn't work using try and catch (if you use this method to view the php version) or if you know that this path is changing Once a day, you can create a file that is launched using the procedure.

If you do this, you need to save this path, but I do not know if there is a method for creating a global variable. My solution is to store either in the database or in a file.

I hope I helped you.

+2
source

As far as I understand from the answer fooobar.com/questions/1274054 / ... , you need to include files based on the script directory of the entry point.

The solution to this without using Apache or php.ini auto_prepend_file might be to use getcwd() . It returns the current working directory, which should be the path of the entrypoint / main script, even when invoking incoming files.

Can you try if just using getcwd() instead of $baseDir ?

 include_once getcwd().'/functions/includes.php'; include_once getcwd().'/functions/get.php'; 
+1
source

Using Apache to fix your application solves it at the wrong level.

If you need to update a bunch of files in your code when the file location is changed, this is your code telling you that you made a poor architectural decision. Therefore, correct the actual problem and stop repeating.

Determine where your included people live in one place, and then, when necessary, update this place accordingly.

One way to do this is to set a constant in the root of your application. This assumes that you have some code at the root of your application that is invoked on every page. Most likely this is index.php or the script included by index.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 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 everyone wants to include the config.php , maybe just include it in your index.php .

This is not a "bad" world that you want to avoid. It uses constants as they are intended. Constants are part of the namespace if you use the namespace for your application.

This assumes that there is one subfolder that sometimes changes places and the script somewhere that is part of each request. It's right? If not, submit your directory structure.

The function will also work and may include logic based on things like the path of the calling script:

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

Other scripts may call this function as follows:

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

Then, when the situation changes, you simply update one function in one file.

All this assumes that you have index.php or a similar script that processes each request and may be the only control point for defining a function or constant. If it is not, then it must be!

Updating the code to use the router ( here it is ) will take about 10 minutes and may simplify this gnarly.htaccess file that you published.

0
source

Source: https://habr.com/ru/post/1274054/


All Articles