1

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".

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

  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)

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;
}

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'

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 in advance!

SOLUTION

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.

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.

Community
  • 1
  • 1
itsraghz
  • 857
  • 1
  • 11
  • 25
  • constants don't need quotes – Rupam Dec 21 '14 at 03:26
  • Rupam, thanks. I had actually used a method called getDocumentRoot() to give me the link. I had adjusted that in my post now. – itsraghz Dec 21 '14 at 03:30
  • The problem always been there, at the first time you used include and at second time you used require. the diff between these functions that require throw fatal error if the file not exists and include not throw fatal error. – 4EACH Dec 21 '14 at 04:41
  • @4EACH I am aware of the difference between require and include. However, I had included ONLY require at both the times. I had quoted the SO link for the structuring what I had done. – itsraghz Dec 21 '14 at 05:13
  • what is crrently returning getDocumentRoot() ? What is the path you're expecting ? Your paths should be relative to your App home but you should use absolute path with a constant defined by a file in your home, or a file that is able to define it. – Loenix Dec 21 '14 at 06:17
  • @Leonix, thanks. The getDocumentRoot() returns '/phpPractices/myApp' - which is the root of my Web app. I am surprised that the file exists but fails to include. – itsraghz Dec 21 '14 at 06:27
  • need an absolute path with doc_root like `$_SERVER['DOCUMENT_ROOT'] . '/phpPractices/myApp/includes/connection.php'` – Deadooshka Dec 21 '14 at 07:11
  • @Deadooshka, getDocumentRoot() gives '/phpPractices/myApp' with which I append '/includes/connection.php'. I think they both do the same thing. – itsraghz Dec 21 '14 at 09:18
  • forward slash in `include` path means relative to the file system root. In your case `getDocumentRoot` must return `$_SERVER['DOCUMENT_ROOT'] . $baseDir` – Deadooshka Dec 21 '14 at 19:08
  • @Deadooshka, I don't get you. My getDocumentRoot() returns the appropriate directory location as 'http://localhost/phpPractices/myApp' with which I append '/includes/connection.php'. Anyways I had got a diff solution which I had added at the bottom of my original post above now. – itsraghz Dec 22 '14 at 01:52

1 Answers1

1

Update your getDocumentRoot() function to return absolute path

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

or use dirname(__FILE__) in your require

Gihan
  • 4,163
  • 6
  • 31
  • 49
  • 1
    This will not work, cause _\_FILE_\_ contains the name of the file getDocumentRoot() is in. – Loenix Dec 21 '14 at 06:18
  • Thanks @G45. Leona is right, as the __FILE__ will give the corresponding file's directory. Hence, I have a diff implementation of getDocumentRoot() which I had customised and shared above in my post. The file physically exists but the file_exists() method returns false. – itsraghz Dec 21 '14 at 07:05
  • @itsraghz pls list your directory structure from webroot, where files containing getDocumentRoot() and require_once getDocumentRoot() . '/includes/connection.php', – Gihan Dec 21 '14 at 07:16
  • @G45, thanks. I have added the directory structure to my post above. Appreciate your support. – itsraghz Dec 21 '14 at 09:26
  • @G45, thanks for your help! I had found out a different solution and I had pasted the same at the bottom section of my original post. – itsraghz Dec 22 '14 at 02:00