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?
9 Answers
Answers 1
You can use auto_prepend_file directive in your .htaccess to make a .php file included before all of your php files.
Have this in your .htaccess:
php_value 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'; Answers 2
__DIR__ is certainly what you are looking for. Use it to provide relative paths for required files. I would highly suggest using require_once or include_once for all library files.
include_once dirname(dirname(__DIR__))."/include/config.php"; Answers 3
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.
Answers 4
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.
Answers 5
This is how I would 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 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.
Answers 6
Refer this link hope it helps http://www.geeksengine.com/article/php-include-path.html
Answers 7
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" Answers 8
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.
Answers 9
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 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 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 the config.php file, maybe just go ahead and include it in your index.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 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.
0 comments:
Post a Comment