including multiple files in different directories php

17,397

You have a couple of options here - either by changing your relative links to properly reach up to the folder they are supposed to be in, or by changing the current working directory to the appropriate folder using the chdir() method. You could also use absolute paths.

How you should change your relative links is dependent on where in the directory structure you are accessing the file from...

Assuming you're hitting your blog file something like this:

http://www.site.com/blog/blog.php

And that your Admin folder is Inside the blog folder;

I would include the files like so:

include "admin/db.php";
include "admin/functions.php";

Again - assuming that both of these files are inside of the admin folder inside of the blog folder;

I.E. root/blog/admin/db.php

This is how I am interpreting what you are saying...

Share:
17,397
rodzilla
Author by

rodzilla

Updated on June 04, 2022

Comments

  • rodzilla
    rodzilla almost 2 years

    I am have trouble including files containing functions. I have a file called blog.php that runs the following:

    <?php
    require 'db.php';
    require 'functions.php';
    
    //Connect to the DB
    $conn = Blog\DB\connect($config);
    if ( !$conn ) die('Problem connecting to the db.');
    

    The blog.php file is in the blog folder. Inside the blog folder is an admin folder containing an index.php file with the following code:

    <?php
    require '../blog.php';
    

    This should allow me to call the blog.php which then calls the db.php file which contains my database connection functions but I am getting an error that the connect function is undefined . It's trying to require db.php from the blog>admin folder, not from where the blog.php file is located (which is the same directory as db.php)? How can I fix this?