tags:

views:

51

answers:

4

Hi

I am trying to include a simple script like this :

ini_set('include_path', ini_get('include_path').':'.dirname(__FILE__));
include('../configuration/live/database.php');

When i run the script (with sudo), PHP returns :

PHP Warning: include(../configuration/live/database.php): failed to open stream: No such file or directory in /home/adil/workspace-netbeans/trunk/applications/dudu/scripts/ejabberd_populate.php on line 17

For sake of clarification, database.php does exist at

/home/adil/workspace-netbeans/trunk/applications/dudu/configuration/live/database.php

In my script, dirname(_FILE_) returns

/home/adil/workspace-netbeans/trunk/applications/dudu/scripts

Hence, the following works :

include(dirname(__FILE__).'/../configuration/live/database.php')

What i'm trying to understand is why my code (with include_path) didn't work? Is there a way to see how the include files are being searched in the include_path?

This same script works on our production server just fine. It follows the same file hierarchy.

Note : The easiest solution is to define a 'SITE_ROOT' and prepend the includes, but that is not the reason for this post. I want to know why the above doesn't work.

PS : I am using PHP 5.3.2 on Ubuntu 10.04

+1  A: 

It doesn't work because the file doesn't exist at that path.

Daniel Egeberg
It does exist at dirname(__FILE__).'../configuration/live/database.php'. Ie: '/home/adil/workspace-netbeans/trunk/applications/dudu/scripts/../configuration/live/database.php'
Adil
So the script is located in '/home/adil/workspace-netbeans/trunk/application/dudu/configuration/live/database.php'?
faileN
yes. That is the correct full path
Adil
Excellent answer. Sometimes we have to get such ones. @Adil what if you try to include both - a full one and a relative one? Just check yourself. Your include path magic doesn't matter. just set correct path, relative to currently called script. Your question is not some special knowledge matter but just paying little more attention matter
Col. Shrapnel
@Col. Shrapnel - I am aware that require(__DIR__."/../configuration/live/master.php") works. I don't need to "get such ones" . Read the question again. Why does the first code statement in my question not work.
Adil
@Adil you have got your answer already. **the file doesn't exist at that path.** read it again. Then do some **debugging** to find the problem. Including the full absolute path is the part of such investigation, not the solution suggested, as you may think.
Col. Shrapnel
@Col. Shrapnel - Yes, i have TESTED WITH THE FULL ABSOLUTE PATH in the 'include' and it WORKS. Knowing that the FILE DOES EXIST AT THAT PATH only then have i bothered to post the question to this site. Don't bother replying if you get offended when your answer is not right. If you have any constructive thoughts to debug, then i am willing to listen.
Adil
@Adil did you try file_exists(), glob(), is_readable()? The problem still is undiscovered, so, no proper solution can be found. This behavior is quite odd, and it would be nice to find the root of the problem. Or a mistype that caused this.
Col. Shrapnel
A: 

The above answers are drifting away from the real question.

For the sake of someone having a similar problem, here's the immediate solution :

require(dirname(__FILE__)."/../configuration/live/master.php");

In PHP 5.3 you can also do

require(__DIR__."/../configuration/live/master.php");
Adil
You don't know the problem. So, your solution will help no one.
Col. Shrapnel
A: 

You can do:

require( dirname( dirname(__FILE__) ).'/configuration/live/master.php' );enter code here`

If you need to go "deeper" upwards, just wrap more dirname()

clinisbut
A: 

Ok. I finally found the problem.

I was running the script via shell and it turns out your relative path include works based on your current directory (pwd) in the shell.

So this code :

sudo php workspace-netbeans/trunk/applications/dudu/scripts/ejabberd_populate.php 

didnt work, because my pwd in the shell was my home and the path was not resolving from there. But this

cd workspace-netbeans/trunk/applications/dudu/scripts/
sudo php ejabberd_populate.php 

works.

I'm not sure why the include_path didn't work.

Thank you all, esp. Col. Shrapnel, for helping me out on this seemingly stupid question.

Adil