PHP的问题包括全局路径 [英] Issue with PHP include with global path

查看:305
本文介绍了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 ,基于(文件)等,

  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.

我一直在干涉这两者。对于文件包含,getDocumentRoot()的/ phpPractices / myApp输出失败,因为它试图在根目录(/ 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.

谢谢大家。非常感激。

Thanks everyone. Much appreciated.

推荐答案

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

Update your getDocumentRoot() function to return absolute path

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

或使用 dirname(__ FILE __)在您的要求

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆