如何在php中使用和读取不同文件夹中的php文件?

如何在php中使用和读取不同文件夹中的php文件?,php,mysqli,document-root,Php,Mysqli,Document Root,我想在文件夹中对我的.php文件进行分类。 我有一个根目录,包含文件夹和php文件,如下所示 core initial.php // database connection css style.css js js.css apply index.php apply.php templates head.php footer.php index.php 我的root index.php,我只是像往常一样包含该文件。注*-head.php和footer

我想在文件夹中对我的.php文件进行分类。 我有一个根目录,包含文件夹和php文件,如下所示

core 
   initial.php // database connection
css
   style.css
js 
   js.css
apply
   index.php
   apply.php
templates
   head.php
   footer.php
index.php
我的root index.php,我只是像往常一样包含该文件。注*-head.php和footer.php包含HTML文件

<?php
    require_once 'core/initial.php';
    include 'templates/head.php';

    echo '<a href='apply/index.php'>Apply</a>';    

    include 'templates/footer.php';
?>

但我的问题是,apply文件夹中的index.php文件无法调用initial.php

警告:require_once(core/initial.php):无法打开流:第2行的C:\xampp\htdocs\movement\production\index.php中没有此类文件或目录

致命错误:require_once():无法打开所需文件 中的“core/initial.php”(include_path='\xampp\php\PEAR) 第2行的root\test\apply\index.php

我的apply/index.php代码

<?php
    require_once '../core/initial.php';

    include '../templates/overall/header.php';

    // PHP code here

    include '../templates/overall/footer.php';

为什么不总是基于根链接,就像:

    require_once '/core/initial.php';
    include '/templates/head.php';

为什么不总是基于根链接,就像:

    require_once '/core/initial.php';
    include '/templates/head.php';

相对路径不适用于Windows。。。在linux上确实如此

最好是在名为defines.php的根文件中定义一个常量,并在index.php中使用require_调用它一次

定义.php

DEFINE('_APP_ROOT_','c:\xampp\...');
require_once _APP_ROOT_ . 'core/initial.php';
include      _APP_ROOT_ . 'templates/overall/header.php';
之后,您将在所有require和include中使用此绝对路径,如:

apply/index.php

DEFINE('_APP_ROOT_','c:\xampp\...');
require_once _APP_ROOT_ . 'core/initial.php';
include      _APP_ROOT_ . 'templates/overall/header.php';

您可以在此处找到更多信息:

相对路径不适用于Windows。。。在linux上确实如此

最好是在名为defines.php的根文件中定义一个常量,并在index.php中使用require_调用它一次

定义.php

DEFINE('_APP_ROOT_','c:\xampp\...');
require_once _APP_ROOT_ . 'core/initial.php';
include      _APP_ROOT_ . 'templates/overall/header.php';
之后,您将在所有require和include中使用此绝对路径,如:

apply/index.php

DEFINE('_APP_ROOT_','c:\xampp\...');
require_once _APP_ROOT_ . 'core/initial.php';
include      _APP_ROOT_ . 'templates/overall/header.php';

你可以在这里找到更多信息:

你这样试过吗

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php';
include $_SERVER['DOCUMENT_ROOT'] . '/templates/head.php';

你试过这样吗

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php';
include $_SERVER['DOCUMENT_ROOT'] . '/templates/head.php';

initial.php
或其他帮助文件中,执行以下操作:

/**  List of all dirs you wish to include from, relative to the document root  */
const INCLUDE_DIRS = [
  '/',
  '/core/',
  '/apply/',
  '/templates/'
];

function getPath($filename){
  $rootDir = $_SERVER['DOCUMENT_ROOT'];
  $path = null;

  foreach(INCLUDE_DIRS as $dir){
    if(file_exists($path =$rootDir . $dir . $filename)) return $path;
  }      
  throw new Exception("Could not find $filename in any of the INCLUDE_DIRS directories");
}
现在,你只需要获得一个正确的包含;保存函数的文件。始终使用完整路径将其包括在内,以便它可以在任何位置工作:

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php'
您可以进一步使用指令,该指令告诉PHP在运行正则脚本之前始终执行该文件。这是一个具有上述函数的好文件,PHP将自动为您包含它

无论如何,一旦包含了带有
getPath()
的文件,就不必担心再次使用正确的路径。只需确保所有include目录都列在
include\u DIRS
常量中,并且包含/需要如下文件:

require_once getPath('footer.php'); //getPath will scan INCLUDE_DIRS and find it

请注意,这种方法的一个重要限制是,如果您有两个同名文件,它将不起作用,因为找到的第一个文件将包括在内。在这种情况下,只要确保在
initial.php
或其他帮助文件中包含/要求从
$\u SERVER['DOCUMENT\u ROOT']
开始时始终使用完整路径,请执行以下操作:

/**  List of all dirs you wish to include from, relative to the document root  */
const INCLUDE_DIRS = [
  '/',
  '/core/',
  '/apply/',
  '/templates/'
];

function getPath($filename){
  $rootDir = $_SERVER['DOCUMENT_ROOT'];
  $path = null;

  foreach(INCLUDE_DIRS as $dir){
    if(file_exists($path =$rootDir . $dir . $filename)) return $path;
  }      
  throw new Exception("Could not find $filename in any of the INCLUDE_DIRS directories");
}
现在,你只需要获得一个正确的包含;保存函数的文件。始终使用完整路径将其包括在内,以便它可以在任何位置工作:

require_once $_SERVER['DOCUMENT_ROOT'] . '/core/initial.php'
您可以进一步使用指令,该指令告诉PHP在运行正则脚本之前始终执行该文件。这是一个具有上述函数的好文件,PHP将自动为您包含它

无论如何,一旦包含了带有
getPath()
的文件,就不必担心再次使用正确的路径。只需确保所有include目录都列在
include\u DIRS
常量中,并且包含/需要如下文件:

require_once getPath('footer.php'); //getPath will scan INCLUDE_DIRS and find it

请注意,这种方法的一个重要限制是,如果您有两个同名文件,它将不起作用,因为找到的第一个文件将包括在内。在这种情况下,只要确保在包含/要求从
$\u服务器['DOCUMENT\u ROOT']

开始时始终使用完整路径,就可以查看
$\u服务器['DOCUMENT\u ROOT']
(\uu DIR\uuuuuu)
中的
要求/包括
命令,如
要求($\u服务器['DOCUMENT\u ROOT']./core/initial.php'))
尝试添加
/
如下:
需要一次“/../core/initial.php”
或总是从文档根目录开始,使用
//file.php
我读过一篇文章并观看了有关它的视频,但我几乎不知道如何做。你能更新我上面的代码吗?谢谢查看
require/include
命令的
$\u服务器[“DOCUMENT\u ROOT”]
(\uu DIR\uuu)
,例如
require($\u服务器[“DOCUMENT\u ROOT”]./core/initial.php')
尝试添加
/
这样的命令:
require\u once./../core/initial.php'
或总是从文档根目录开始,使用
//file.php
我读过一篇文章并观看了有关它的视频,但我几乎不知道如何做。你能更新我上面的代码吗?谢谢操作注意事项:@AlotJai如果您使用此选项,请记住
DEFINE(“'u-APP\u-ROOT','c:\xampp\…”)
如果您将应用程序部署到其他地方(如服务器上),则会中断,因此请说明操作注意事项:@AlotJai如果您使用此选项,请记住
DEFINE('u-APP\u-ROOT','c:\xampp\…)
如果您将应用程序部署到其他地方(如服务器上),则会出现故障,因此请说明这一点性能如何!?我认为做一些循环来调用每个php文件不是最好的主意@梅洛曼:这是一个公平的观点。如果迭代通过几个目录是不可接受的,那么OP应该考虑我的最后一个段落或者实现一个哈希图(关联数组)常量的<代码>文件名'= >文件> <代码>要被<代码> GETPATH()/<代码>函数WHOLL消耗,同时,有多少CMS像Drupal使用这个解决方案!我讨厌它,但它很普遍。那性能呢!?我认为做一些循环来调用每个php文件不是最好的主意@梅洛曼:这是一个公平的观点。如果迭代通过几个目录是不可接受的,那么OP应该考虑我的最后一个段落或者实现一个哈希图(关联数组)常量的<代码>文件名'= >文件> <代码>要被<代码> GETPATH()/<代码>函数WHOLL消耗,同时,多少CMS?