0

I have the following folder structure for my site:

    Admin/
    Tech/
index.php
    classes/
    include/
    core/

Everything seems to work except for when i try to call the following code from Admin/index.php

<?php
#including our init.php
require '../core/init.php';
$general->logged_in_protect();
?>

I get the following error for some reason. I can't figure out what to do to fix it or if its even possible to fix.

The core/init.php file has the following code:

<?php
    #starting the users session
    session_start();
    require 'include/database.php';
    require 'classes/users.php';
    require 'classes/general.php';

    $users = new Users($db);
    $general = new General();

    $errors = array();
?>

error that i am receiving:

PHP Warning:  require(include/database.php): failed to open stream: No such file or directory in 

Can someone please help me with this?

4
  • could you please add some indent or space to make your folder structure more readable? Commented Dec 26, 2013 at 19:51
  • ok i added some indent space, hopefully that helps. Commented Dec 26, 2013 at 19:53
  • These all directories are at the same level? Commented Dec 26, 2013 at 19:56
  • yes they are all at the same level Commented Dec 27, 2013 at 1:57

2 Answers 2

1

You probably just need:

require '../include/database.php';
require '../classes/users.php';
require '../classes/general.php';

But if you use a lot of relative paths, it can get messy real quickly so you might want to consider using your base-directory like for example (you can also use a variable or a constant for that):

require $_SERVER['DOCUMENT_ROOT'] . '/include/database.php';
require $_SERVER['DOCUMENT_ROOT'] . '/classes/users.php';
require $_SERVER['DOCUMENT_ROOT'] . '/classes/general.php';
Sign up to request clarification or add additional context in comments.

4 Comments

I tried what you said which works perfect, but i ran into a weird issue though, i have the following section: if($_SESSION['role'] == "Admin"){ header('Location: ' . $_SERVER['DOCUMENT_ROOT'] . '/admin/index.php'); }else{ header('Location: ' . $_SERVER['DOCUMENT_ROOT'] . '/tech/index.php'); } but for some reason when it tries to redirect instead of going to document_root/admin/index.php it goes to document_root/admin/admin/index.php
@BradHazelnut No, in your header() call you need to use the path relative to the web-root, not the file-system. So there it is just '/tech/index.php', possibly with the domain name in front of it.
I changed the code to just be tech/index.php and it still does that not sure why its doing that, any ideas?
@BradHazelnut You need an absolute path like /tech/index.php.
0

If you use paths relative to current script location, like this:

<?php

require(dirname(__FILE__).'/../core/other.inc');

...
?>

You always have it under control, regardless of the include_path PHP ini setting.

In your case you should have the following:

<?php
  #including our init.php
  require(dirname(__FILE__).'/core/init.php');
  $general->logged_in_protect();
?>

and core/init.php will look like:

<?php
    #starting the users session
    session_start();
    require(dirname(__FILE__).'/../include/database.php');
    require(dirname(__FILE__).'/../classes/users.php');
    require(dirname(__FILE__).'/../classes/general.php');

    $users = new Users($db);
    $general = new General();

    $errors = array();
?>

Hope this helps.

1 Comment

awesome, that sounds like exactly what i need. would you be able to give me an example on how i can use it with the my code please?

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.