Bill
Bill

Reputation: 571

"current directory" notation not working

I was taught that it's good practice to specify the directory of a file you're linking to, even when the file is in the current directory. So, for example, use "./constants.inc.php" as opposed to "constants.inc.php". This has generally worked fine for me, except in one particular spot in my code I get the following PHP error when I use the ./ notation:

Warning: require(./constants.inc.php) [function.require]: failed to open stream: No such file or directory in C:\xampp\htdocs\widget_corp\includes\functions.inc.php on line 4

Fatal error: require() [function.require]: Failed opening required './constants.inc.php' (include_path='.;C:\xampp\php\PEAR;C:\xampp\htdocs\websites\mathverse\includes') in C:\xampp\htdocs\widget_corp\includes\functions.inc.php on line 4

Writing "./constants.inc.php" gives this error, but "constants.inc.php" doesn't (PHP seems to find the file just fine). Can someone tell me why the ./ notation doesn't work in this particular case? Perhaps it's something very simple I'm missing?

To give a bit more context (if necessary), the error is in the function below (line 2), which is in my PHP functions file (called functions.inc.php) which is inside an includes directory. The constant.inc.php file is also inside the includes directory.

Thank you.

function connect_to_db() {
  require("./constants.inc.php");
  $connection_id = mysql_connect(DB_SERVER, DB_USER, DB_PASS);
  if (!$connection_id) {
    die("Database connection failed: " . mysql_error());
  }
  $db_selection_was_successful = mysql_select_db(DB_NAME, $connection_id);
  if (!$db_selection_was_successful) {
    die("Database selection failed: " . mysql_error());
  }
}

Upvotes: 2

Views: 806

Answers (3)

Zevi Sternlicht
Zevi Sternlicht

Reputation: 5399

Create the following heirachy:

index.php // echo 'I am in the top directory';
folder1/
    index.php // echo 'I am in folder 1';
    folder2/
        index.php // 'echo I am in foler' 2

on your top level index.php require folder1/index.php. From that require 'folder2/index.php';

This works no problem. However add a ./ before the folder2/index.php and you will get an error. It seems as once you specify which folder you want by using the ., it has to be relative to the current working directory. But if left open, the path will be checked in the calling scripts own directory too.

From the manual:

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

Upvotes: 0

genesis
genesis

Reputation: 50974

even with or without ./ PHP will be searching for same file (in case of ./, it won't search in include_path) , and this means that constats.inc.php DOES NOT exist.

Try this one

require(dirname(__FILE__)."/../constants.inc.php");

Upvotes: 3

Pekka
Pekka

Reputation: 449605

Writing "./constants.inc.php" gives this error, but "constants.inc.php" doesn't (PHP seems to find the file just fine). Can someone tell me why the ./ notation doesn't work in this particular case? Perhaps it's something very simple I'm missing?

The only explanation I have is that PHP seems to be finding the constants.inc.php file in one of the directories in the include path - most likely C:\xampp\htdocs\websites\mathverse\includes.

Upvotes: 3

Related Questions