tags:

views:

63

answers:

4

When I use ../mysqlConnect.php I get the following messages.

Warning: require_once(../mysqlConnect.php) [function.require-once]:
failed to open stream: No such file or directory in /home/content/etc...  

Fatal error: require_once() [function.require]: Failed opening required 
'../mysqlConnect.php' (include_path='.:/usr/local/php5/lib/php') in /home/content/etc...    

When I use the directory name - mydir/mysqlConnect.php - everything works fine.

A: 

That's because you are not specifying the correct include path. ../ refers to parent directory. ../../ goes two directories back, ../../../ goes three of them back. If the mysqlConnect.php file is present in the same folder as your script, you don't need to specify ../ in the include.

Make sure that you specify the correct path. You can easily check whether or not you are specifying correct path like:

if (file_exists('../mysqlConnect.php'))
{
  echo 'Iam specifying the correct path !!';
}
else
{
  echo 'Well, I am not :(';
}
Sarfraz
The file I want to include is in a folder one level below the root directory. What is the correct path for that?
aliov
@aliov: If it is one level below from the root and your script is in the root then you should specify the name of that folder rather than `../`.
Sarfraz
A: 

because it doesn't find your file then. to give a more specific answer I need to see you file-/folder-structure

jigfox
The file I want to include is in a folder one level below the root directory.
aliov
and where is the file that wants to include this file? it doesn't matter where the file is relative to the root directory. What matters is its position relative to the file that want's to include the other file.
jigfox
@Jens: The script that is requiring the file is in the root dir. The file I require is in a folder one level below the root.
aliov
as per definition there can't be anything below the root folder. The root is the first. Please draw a tree of your structure like [BoltClock](http://stackoverflow.com/users/106224/boltclock) did in his [answer](http://stackoverflow.com/questions/3039873/why-do-i-get-a-warning-and-a-fatal-error-when-i-use/3039911#3039911)
jigfox
+1  A: 

Require is relative to the invoced script, not the script you call require() in. Use something like this to have an absolute path:

 require(dirname(__FILE__) . '/../mysqlConnect.php');

In PHP 5 you can also use DIR.

ZeissS
+2  A: 

require_once('../mysqlConnect.php') asks PHP to look in the directory above the one your script is currently in for mysqlConnect.php.

Since your connection file appears to be in a mydir directory, require_once('mydir/mysqlConnect.php') works because it looks in that directory, which is contained by the one it's currently in.

Visual representation (assuming script.php is your script including that file):

dir/
  subdir/              # PHP looks here for ../mysqlConnect.php
    script.php
    mydir/             # PHP looks here for mydir/mysqlConnect.php
      mysqlConnect.php
BoltClock
The script that is requiring the file is in the root dir. The file I require is in a folder one level below the root.
aliov