php 如何相对路径
php how to relative pathing
如何做相对路径?如何使用 __dir__
这是我之前的文件结构
public_html
├── config
| |__config.php
├── core
| |__init.php
├── helper
| |__helper.php
├── libraries
| |__page1.php
├── templates
│ ├── page1template.php
│
│__index.php
│__page1.php
页面太多之后。我想将它们整理到 "rep" 文件夹和 "admin" 文件夹中。所以我的新文件结构是这样的
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
问题是,我的要求和包含仍然是绝对路径。请参阅下面的示例
我的初始化文件
//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');
}
我的page1.php
<?php require('core/init.php'); ?>
//Get Template & Assign Vars
$template = new Template('templates/rep/page1.php');
如果我在每条路径前加一个“../”。然后页面将正确加载。见下文
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');
但是我的 index.php 会中断,因为索引位于根文件夹中,因此新路径相对于它是错误的。我发现了两个与相关问题相关的问题。我怀疑它与使用 __dir__
替换 ../ 有关,但我不知道放在哪里。
PHP include relative path
Are PHP include paths relative to the file or the calling code?
就我个人而言,如果我确定结构应该是某种方式,我会尝试相对于执行包含的文件进行包含。例如,您可以在 core/loader.php 紧挨着 core/init.php 创建一个文件 ... 然后,如果您假设加载器和另一个加载器之间的相对结构文件将始终保持不变,core/loader。php 可能类似于
<?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
?>
这里的要点是,您现在可以将这组文件夹移动到任何您想要的位置(建议:在 public_html 之外),只要您需要正确的路径,它们仍然应该加载装载机.
如何做相对路径?如何使用 __dir__
这是我之前的文件结构
public_html
├── config
| |__config.php
├── core
| |__init.php
├── helper
| |__helper.php
├── libraries
| |__page1.php
├── templates
│ ├── page1template.php
│
│__index.php
│__page1.php
页面太多之后。我想将它们整理到 "rep" 文件夹和 "admin" 文件夹中。所以我的新文件结构是这样的
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
问题是,我的要求和包含仍然是绝对路径。请参阅下面的示例
我的初始化文件
//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');
}
我的page1.php
<?php require('core/init.php'); ?>
//Get Template & Assign Vars
$template = new Template('templates/rep/page1.php');
如果我在每条路径前加一个“../”。然后页面将正确加载。见下文
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');
但是我的 index.php 会中断,因为索引位于根文件夹中,因此新路径相对于它是错误的。我发现了两个与相关问题相关的问题。我怀疑它与使用 __dir__
替换 ../ 有关,但我不知道放在哪里。
PHP include relative path
Are PHP include paths relative to the file or the calling code?
就我个人而言,如果我确定结构应该是某种方式,我会尝试相对于执行包含的文件进行包含。例如,您可以在 core/loader.php 紧挨着 core/init.php 创建一个文件 ... 然后,如果您假设加载器和另一个加载器之间的相对结构文件将始终保持不变,core/loader。php 可能类似于
<?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
?>
这里的要点是,您现在可以将这组文件夹移动到任何您想要的位置(建议:在 public_html 之外),只要您需要正确的路径,它们仍然应该加载装载机.