dongmi5177 2016-06-27 00:18
浏览 54
已采纳

php如何相对路径

How to do relative pathing? how to use __dir__

here is my file structure before

public_html
  ├── config
  |    |__config.php
  ├── core
  |    |__init.php
  ├── helper
  |    |__helper.php
  ├── libraries
  |    |__page1.php
  ├── templates
  │   ├── page1template.php
  │
  │__index.php
  │__page1.php

After having too many pages. I want to tiddy them up into "rep" folder and "admin" folder. so my new file structure look like this

public_html
  ├── config
  |    |__config.php
  ├── core
  |    |__init.php
  ├── helper
  |    |__helper.php
  ├── libraries
  |    |__page1.php
  ├── templates
  │   ├── page1template.php
  │
  │__index.php
  |
  ├── rep
  |    |__page1.php
  ├── admin
  │   ├── page1.php

The problem is, my requires and includes are still absolute pathing. see a sample below

my init file

//Start Session
session_start();

//Include Configuration
require_once('config/config.php');

//Helper Function Files
require_once('helpers/system_helper.php');
require_once('helpers/format_helper.php');

//Autoload Classes
function __autoload($class_name){
    require_once('libraries/'.$class_name .'.php');
} 

my page1.php

 <?php require('core/init.php'); ?>
     //Get Template & Assign Vars
     $template = new Template('templates/rep/page1.php');

if I add an "../" in front of every path. then the page will loads correctly. see below

require_once('../config/config.php');
require_once('../helpers/system_helper.php');
require_once('../helpers/format_helper.php');
require_once('../libraries/'.$class_name .'.php');
require('../core/init.php'); 
$template = new Template('../templates/rep/page1.php');

but then my index.php will break because index is in the root folder, so the new path is wrong relative to it. I found two question with related issue. I suspect it is related to using __dir__ to replace ../ but I dont know where to put it.

PHP include relative path

Are PHP include paths relative to the file or the calling code?

  • 写回答

1条回答 默认 最新

  • dongyi1015 2016-06-27 00:58
    关注

    For me personally, I try to make includes relative to the file doing the including, if I'm sure the structure is supposed to be a certain way. For instance, you could make a file at core/loader.php right next to core/init.php ... Then if you assume the relative structure between the loader and the other files would always remain the same, core/loader.php could be something like

    <?php
    
    // Get the path of whatever folder is holding the core/config/etc folders
    $main_dir=RealPath(DirName(__FILE__).'/../');
    
    require("{$main_dir}/config/config.php");
    require("{$main_dir}/helpers/system_helper.php");
    // etc
    
    ?>
    

    The point here being that you can now move that set of folders anywhere you want (recommendation: outside public_html), and they should still load, as long as you require'ed the right path to the loader.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题