【问题标题】:Get the root folder path in php using include使用include获取php中的根文件夹路径
【发布时间】:2013-02-25 15:14:57
【问题描述】:

我在 c:\wamp\www\ 中有一个名为 mywebsite 的 php web 应用程序。 该文件夹由 featured/fashionWeek/database/Mysql.php 文件组成。我使用 dirname(FILE) 函数从根目录获取文件夹的路径,以便我可以将文件夹放置到实时服务器的 public_html 文件夹中的任何位置。

它在我的本地主机中运行良好。我在 wamp\www\mywebsite\featured\fashionWeek\database\Mysql.php 中有以下代码 include(dirname(FILE); 工作正常,返回此路径:C:/wamp/www/mywebsite /特色/时尚周/数据库

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

但是当我将 mywebsite 文件夹放在实时服务器的 public_html 文件夹下时。它给出了以下错误:

Warning: include(/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php) [function.include]: failed to open stream: No such file or directory in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

Fatal error: include() [function.include]: Failed opening required '/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

【问题讨论】:

  • 我认为include(dirname(__FILE__).'/config/Dbconfig.php') 有效

标签: php include


【解决方案1】:

要倒退,即“..”,您可以使用 dirname 函数。

<?php
include(dirname(dirname(__FILE__)).'/config/Dbconfig.php');

应该可以。

更多

最终管理这些会很烦人,所以我建议在您的原始文件中定义一个 ROOT 常量。这将包含项目根目录的绝对路径。这很简单。

defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);

【讨论】:

    【解决方案2】:

    https://stackoverflow.com/a/2152320/1528331

    看看是否有帮助。还有,

    dirname(__FILE__) is the same as __DIR__
    

    【讨论】:

      【解决方案3】:

      有点松弛,但它可能会帮助你

      if(!function_exists('getRootdir'))
      {
      function getRootdir($NFILE)
      {
      $filefitter="";
      while(!file_exists($filefitter.$NFILE))
      $filefitter="../".$filefitter;
      return $filefitter;
      }
      }
      

      使用

      like  $rootpath=getRootdir("Root identifier folder / php file");
      

      【讨论】:

      • 什么是根标识符文件夹?你能解释一下吗
      • 我说这是 Little Bit Flabby,如果您的根文件夹包含任何可识别的文件或文件夹,那么代码将对其进行跟踪,我用这个 2 年前找到根文件夹,我的根文件夹包含一个文件夹“操纵杆”给出了它,以便 fn 返回相对于包含我使用的文件的根文件夹 $root=getRootdir("joystick");包括 ($root ."rootinclude.php");
      猜你喜欢
      • 2021-02-05
      • 2012-04-09
      • 2011-07-31
      • 2011-10-29
      • 2015-06-15
      • 2019-07-13
      • 1970-01-01
      • 2018-10-03
      • 2014-02-12
      相关资源
      最近更新 更多