我们如何在不指定子文件夹路径的情况下包含php文件 [英] How can we include php files without specifying the subfolder path

查看:90
本文介绍了我们如何在不指定子文件夹路径的情况下包含php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常我们使用以下代码在彼此之间包含php文件:

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...
?>

但是以上代码仅在php文件位于根文件中时适用。我的意思是,如果我们将文件移动到子文件夹中。我们需要对包含在php文件中的代码进行更改。例如:

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...
?>

我的意思是,当我们将php文件移到子文件夹中时,include_once要请参阅子文件夹名称,例如( include_once'subfolder / include / config.php'; )。这是一个具有挑战性的情况,因为我们需要对许多文件中的包含页面执行此操作。

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.

例如,我包含 include_once $ _SERVER [' DOCUMENT_ROOT']。'/ functions / includes.php'; 来自 index.php ,并且还包括了所有php文件中的includes.php文件例如 header.php,posts.php和ajax_post.php 。从根文件夹可以正常工作,但是如果我们将文件移动到子文件夹中,那么include.php文件将不包含没有子文件夹名称的文件。

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.

也许也可以通过 .htaccess 来做到这一点。

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

我已经制作了此htaccess代码,也许您有使用htaccess的解决方案。我必须说我曾尝试使用 RewriteBase / subfoldername / ,但包含文件没有起作用。

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]

我的责任是,如何包含不带子文件夹名称的php文件?

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

推荐答案

您可以在.htaccess中使用 auto_prepend_file 指令,使 .php 文件包含在所有PHP之前文件。

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

如果您使用的是 mod_php ,则在.htaccess中输入以下内容:

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

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

注意,对于 PHP-FPM ,您需要在 .user.ini 文件:

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"

然后创建一个新文件 env。 php 在项目基本目录中只有一行:

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

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

此行设置变量 $ baseDir 具有项目基础目录的值,即 / Applications / MAMP / htdocs / script / OR / Applications / MAMP / htdocs / subdir / script /

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/

然后在需要的任何地方使用此变量 $ baseDir 包括其他文件,例如:

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';

这篇关于我们如何在不指定子文件夹路径的情况下包含php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆