astral
astral

Reputation: 53

How to properly require a file in another directory?

In line 3, I use the following:

require'./inc/connection.php' or die ("Cannot find the proper configuration    file."); 

I get this error:

PHP Warning:  require_once(1): failed to open stream: No such file or directory in (directory)

If anybody could help, this is how I have it setup.

index.php is the file that is using the require. connection.php is in the "inc" directory.

Upvotes: 1

Views: 96

Answers (4)

Saurabh
Saurabh

Reputation: 784

You need to put two dots when you want to traverse one directory back.here you have index.php and inc in same directory so you should write

require 'inc/connection.php' or die ("Cannot find the proper configuration file."); 

Upvotes: 0

unixmiah
unixmiah

Reputation: 3153

You can also have it this way, just in case if the path changes if you move the source code around.

$basePath = "/systemdir";
$incPath  = $basePath."/inc/";
require $incPath.'connection.php' or die ("Cannot find the proper configuration file.");

require $incPath.'connection.php';

You really don't need a die at the end to check, php will automatically show an error if the file isn't found in that directory.

If you're on windows, try this

require __DIR__ . '/connection.php';

require 'inc/connection.php';

Upvotes: 1

offchance
offchance

Reputation: 682

When you're including a file, you can either:

1- Specify the full path for that file (starts with /)

For example

require '/var/www/html/site-name/site/lib/inc/connection.php' or die ("Cannot find the proper configuration    file.");

Notice that /var/www/site-name/site/lib/ part? That's made up for this example. The real absolute path depends on the server and where that desired folders reside.

2- Specify the relative path for that file (starts with a folder or file name)

For example

require '../inc/connection.php' or die ("Cannot find the proper configuration    file.");

Using the relative path means you're starting from the folder where the code actually exists. For example, the path: 'connection.php' means connection.php resides in the same current folder.

The special names . and .. mean current and parent directory/folders respectively. So to include a file named connection.php that resides in the parent folder of where the code calling it you can target it by ../connection.php.

So, in order to fix your issue, you need to identify where is that particular file you're trying to target relatively. And it looks like the current path starts with . which means the current folder (which doesn't make sense).

An easy guess is that it was supposed to be .. instead ., but not necessarily the case. The file could've been moved since that code was implemented, or a folder like inc name changed.

Upvotes: 1

Devsi Odedra
Devsi Odedra

Reputation: 5322

put two dots(..) before /inc

Like below. use this code instead of your.

require '../inc/connection.php' or die ("Cannot find the proper configuration    file."); 

Upvotes: 0

Related Questions