douhoulei4706 2012-05-21 09:02
浏览 134
已采纳

后端和前端的站点根目录

Let's say that I have this website file root structure:

  • frontend /home/username/public_html/
  • backend /home/username/public_html/admin/

In frontend folder there is a file called config.php. In this file I get the site root of my website like this:

define('ROOT', dirname(__FILE__));

This file is also included in both index files (index.php) in frontend and backend. But for backend, the root is /home/username/public_html/admin/, which should actually be /home/username/public_html/. How can I do this, in one line, in config.php?

LE:

Demo code http://pastebin.com/LGrrJzaV

  • 写回答

2条回答 默认 最新

  • dswqw66280 2012-05-21 09:10
    关注

    The FILE gives you the full path and filename of the file.

    The dirname() returns the path of the parent directory.

    So, if you use:

    define('ROOT', dirname(__FILE__));
    

    It gives you the directory structure for the location of the current script.

    To be inside the admin/ and have returned the previous folder:

    $path = dirname(__FILE__);
    
    define("ROOT", $path.'/../');
    
    // Outputs:  /home/username/public_html/
    

    In one line being inside admin/:

    define("ROOT", dirname(__FILE__).'/../');
    

    EDITED

    Have the same path for both locations in one line:

    define("ROOT", (strpos(dirname(__FILE__), "admin")>=0) ? (str_replace("admin", "", dirname(__FILE__))) : (dirname(__FILE__)));
    

    This will output on both index.php:

    • frontend /home/username/public_html/ -> /home/username/public_html
    • backend /home/username/public_html/admin/ -> /home/username/public_html
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题