首页 文章

使用include获取php中的根文件夹路径

提问于
浏览
1

我在c:\ wamp \ www \中有一个名为 mywebsite 的php web应用程序 . 该文件夹包含 featured/fashionWeek/database/Mysql.php 文件 . 我使用dirname( FILE )函数从root获取文件夹的路径,以便我可以将文件夹放在live server的public_html文件夹中的任何位置 .

它在我的localhost中工作正常 . 我在wamp \ www \ mywebsite \ featured \ fashionWeek \ database \ Mysql.php里面有以下代码include(dirname( FILE );工作正常,返回此路径:C:/ wamp / www / mywebsite / featured / fashionWeek / database

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

3 回答

  • 0

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

    看看是否有帮助 . 也,

    dirname(__FILE__) is the same as __DIR__
    
  • 0

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

    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");
    
  • 1

    要向后,即'..',您使用 dirname 函数 .

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

    应该管用 .

    更多

    最终管理它们会很烦人,因此我建议在原始文件中定义ROOT常量 . 这将包含项目根目录的绝对路径 . 这很简单 .

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

相关问题