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?
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.