文件在根目录中的路径是什么?


what would be the path for the file in the root directory?

我已经学会了,为了安全起见,将mysqli连接config.php文件放在根目录中(不在公共html中)。如果我想在查询前包含config。php路径是什么?

我的config.php如下

              <?php
              $hostname='';
              $username='';
              $password='';
              $database_name='';

             $mysqli = new mysqli($hostname, $username, $password, $database_name); 
             ?>

在我的其他php文件

             <?include('config.php');?>

my config.php在根目录下。include语句中的路径是什么?

当您说"根目录"时,通常是指文件系统的根目录。在大多数*nix (Linux, FreeBSD, Unix)安装中,这意味着您的文件系统路径只是"/"。也就是说,如果你的配置文件在那里,你的PHP文件应该是这样的:

<?php
    include('/config.php');
?>

然而,这通常是一个坏主意,并且需要超级用户权限才能实际将文件放在文件系统的根目录"/"。所以很可能(但我们不得不猜测),您指的是web服务器的DOCUMENT ROOT。正如Kai Qing在他的评论中提到的,大多数运行PHP的web服务器通过超全局$_SERVER['DOCUMENT_ROOT']提供该值。如果你的文件在那里,那么你可以把它放在你的PHP文件中:

<?php
    include($_SERVER['DOCUMENT_ROOT'] . '/config.php');
?>

参见http://www.php.net/manual/en/reserved.variables.server.php。

你可以试试这个

包括($ _SERVER [' DOCUMENT_ROOT ']。/config . php)

http://css-tricks.com/php-include-from-root/

如果没有正确设置DOCUMENT_ROOT,这个链接还有另一个技巧。