沃梦达 / 编程问答 / php问题 / 正文

PHP包含全局路径的问题

Issue with PHP include with global path(PHP包含全局路径的问题)

本文介绍了PHP包含全局路径的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been facing an issue with the PHP includes. Based on the readings from internet, I have been using relative path to have the flexibility around.

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

As long as I refer to a path directly in each file, like

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

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

The issue starts when I refer to this path with a global constant

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

正如它所说的以下错误.

as it says the below error.

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 中的很多线程并遵循类似的建议

I had been reading so many threads in SO and following the advices like

  1. 使用文件的基本目录定义一个常量作为 DOCUMENT_ROOT.(就像它在 SO 中所说的那样 -> https://stackoverflow.com/a/22912261/1001242)
  2. 还尝试了各种选项,例如 DIR、basedir(FILE) 等,
  3. 通过 set_include_path() 方法将我的应用程序目录 (/phpPractices/myApp) 添加到 include_path 中(如 PHP, 如何设置包含路径 和 http://php.net/manual/en/function.set-include-path.php)
  1. 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)
  2. Also attempted with various options like DIR, basedir(FILE) etc.,
  3. 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()定义如下.

My function getDocumentRoot() is defined as follows.

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,我不明白为什么.有没有可能引起关注?

Note: I could see that the file exists physically. However, file_exists() method returns false, which I don't understand why. Possibly it could have been a cause of concern?

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

FYI, my directory structure is as below.

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 包含以下内容

My list.php contains the below

<?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! :(

?>

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

But nothing helps! Any suggestion to get rid of this issue will be much appreciated.

提前致谢!

解决方案

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

Thanks to all the people whoever had replied. I got a new workaround that kinda helps me :)

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"的目录,这是根本原因.因此,我将它们一分为二,现在可以正常工作了.

I had been meddling with these both. For the file inclusion, the getDocumentRoot()'s output of "/phpPractices/myApp" was failing as it was attempting to locate a directory named 'phpPractices' in the root ("/phpPractices/"), which was the root cause. Hence, I had split these into two and now works fine.

谢谢大家.非常感谢.

推荐答案

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

Update your getDocumentRoot() function to return absolute path

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

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

or use dirname(__FILE__) in your require

这篇关于PHP包含全局路径的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:PHP包含全局路径的问题

基础教程推荐