PHP包含全局路径的问题

本教程将介绍PHP包含全局路径的问题的处理方法,这篇教程是从别的地方看到的,然后加了一些国外程序员的疑问与解答,希望能对你有所帮助,好了,下面开始学习吧。

问题描述

我一直面临 PHP 包含的问题.根据网上的资料,我一直在使用相对路径来获得灵活性.

只要我在每个文件中直接引用一个路径,比如

require_once '../includes/connection.php';  // it works fine.

当我使用全局常量引用此路径时,问题就开始了

require_once getDocumentRoot() . '/includes/connection.php', 

正如它所说的以下错误.

Warning: require_once(/phpPractices/myApp/includes/connection.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/phpPractices/myApp/dir1/list.php on line 19".

我一直在阅读 SO 中的很多线程并遵循类似的建议

    使用文件的基本目录定义一个常量作为 DOCUMENT_ROOT.(就像它在 SO 中所说的那样 -> https://stackoverflow.com/a/22912261/1001242)

    还尝试了各种选项,例如 DIR、basedir(FILE) 等,

    通过 set_include_path() 方法将我的应用程序目录 (/phpPractices/myApp) 添加到 include_path 中(如 PHP, 怎么设置包含路径 和 http://php.net/manual/en/function.set-include-path.php)

    Define a constant as DOCUMENT_ROOT with the base director of the file. (Like the one what it says here in SO -> https://stackoverflow.com/a/22912261/1001242)

    Also attempted with various options like DIR, basedir(FILE) etc.,

    Added my application directory (/phpPractices/myApp) to the include_path via set_include_path() method (as mentioned in PHP, How to set include path and http://php.net/manual/en/function.set-include-path.php)

我的函数getDocumentRoot()定义如下.

function getDocumentRoot()
{
 $loc_pos_2 = strpos($_SERVER['REQUEST_URI'], "/", 1);
 $baseDir = substr($_SERVER['REQUEST_URI'], 0, $loc_pos_2);
 $baseDir = $baseDir . "/myApp";
 return $baseDir;
}

注意: 我可以看到该文件实际存在.但是,file_exists() 方法返回 false,我不明白为什么.有什么可能引起关注?

仅供参考,我的目录结构如下.

htdocs
  phpPractices
 myApp --> Web App Root
includes
connection.php
inc
header.php
footer.php
global.php -- contains getDocumentRoot() method and included in header.php
index.php
dir1 --> my module specific dir
list.php --> which needs 'connection.php'

我的 list.php 包含以下内容

<?php

include_once '../inc/header.php'; // it works fine as I do relative to the path

//require_once '../includes/connection.php'; //works fine
require_once getDocumentRoot() . '/includes/connection.php'; // FAILS! :(

?>

但没有任何帮助!任何摆脱此问题的建议将不胜感激.

提前致谢!

解决方案

感谢所有回复的人.我有一个新的解决方法,对我有帮助:)

1. For all the menu items with links: 

 I use my getDocumentRoot() method which provides me - 
  '/phpPractices/myApp/' --> which is relative to my Web App.

2. For all the include, require functions : 

 I defined a different constant as below & as per this link: 
  https://stackoverflow.com/a/1488293/1001242

 define('APP_BASE_PATH', $_SERVER['DOCUMENT_ROOT'] . '/myApp');

 which returns me the complete absolute path which works fine for file inclusion.

我一直在干预这两者.对于文件包含,/phpPractices/myApp”的 getDocumentRoot() 输出失败,因为它试图在根目录 (/phpPractices/”) 中找到一个名为phpPractices”的目录,这是根本原因.因此,我将它们一分为二,现在可以正常工作了.

谢谢大家.非常感谢.

推荐答案

更新你的 getDocumentRoot() 函数以返回绝对路径

function getDocumentRoot()
{
 return dirname(__FILE__);
}

或在您的要求中使用 dirname(__FILE__)

好了关于PHP包含全局路径的问题的教程就到这里就结束了,希望趣模板源码网找到的这篇技术文章能帮助到大家,更多技术教程可以在站内搜索。

0
没有账号?注册  忘记密码?