1

I have three php files. The "upper" file is a webpage (let's call it Sample.php). At the top of the page it does this:

<body>
<?php include("../Top.php"); ?>
//Webpage content
</body>

The Top.php file is a file with the menu and banner stored, for easy changing. The Top.php file has a line as such:

<?php include("ConnectToDB.php"); ?>

ConnectToDB.php is a file with some php code to connect to a database.

Here, the file system is ordered like so:

  • RootFolder
    • ConnectToDB.php
    • Top.php
    • OtherSample.php
    • Articles
      • Sample.php

When I access Sample.php I get an error in the include("ConnectToDB.php"); inside the include("../Top.php"); statement. but if I have the file OtherSample with the include("Top.php"); statement I will get no error and both Top.php and ConnectToDB.php work perfectly. What is the problem?

5
  • I may be wrong, but I think you must include("../ConnectToDB.php"); on Top.php as well Commented May 3, 2012 at 13:27
  • Sorry for explaining incorrectly, <?php include("ConnectToDB.php"); ?> is inside Top.php Commented May 3, 2012 at 13:34
  • yes, but you will need to add the ../ because the file path is relative to sample.php like radashk and Tobiask said Commented May 3, 2012 at 13:39
  • Yes, I am just looking for a way to do realtive path because more than one page in different directories access it. The relative path by radashk is not working. Commented May 3, 2012 at 13:43
  • I think you didn't untherstand what we said, did you do this include("/RootFolder/ConnectToDB.php") ? change it to include("../ConnectToDB.php") did it work? Commented May 3, 2012 at 13:55

4 Answers 4

5

You could use:

include_once($_SERVER['DOCUMENT_ROOT'].'/RootFolder/whatever.php');

to include files, this way you always have an absolute path for the include and it isn't dependent from where it is called.

Sign up to request clarification or add additional context in comments.

Comments

3

The "include" statements actually ports the code into your page. So keep in mind that include("ConnectToDB.php") is executed from Sample.php, therefore the path is wrong.

The correct line of code would be: include("../RootFolder/ConnectToDB.php")

where .. represent the whole dir structure after "localhost/" or whatever you are using.

2 Comments

Additionally, you should use include_once() top prevent loading the files twice…
It does not work. Though I guessed this was the solution, I get the error <code>No such file or directory</code> for the function include("/RootFolder/ConnectToDB.php") inside Top.php
1

The path is wrong, by calling Sample.php all path belong to the base where the Sample.php is located. PHP searches for ConnectToDB in the wrong folder... Try ../ConnectToDB.php, as from the Sample.php file, this file i one folder above...

Better solution, use an absolute path!

Comments

0

I encountered the same problem. unfortunately,Samson's answer can't solve the problem for me. You could try this one. It works fine for me. Adding a file exist check then add the correct path in your Top.php like this

<?php
if (file_exists("ConnectToDB.php")) {
    include_once("ConnectToDB.php");
}elseif(file_exists("../ConnectToDB.php")){
    include_once("../ConnectToDB.php");
}
?>

then you can access the correct path.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.