Jump to content

PHP path problem with include files


bambinou1980

Recommended Posts

Hello,

 

I would like to know if someone could help me find a solution with this path problem please.

 

I have a file here:

C:\xampp\htdocs\food\admin\crud\customers\add-customers.php

 

In that file I have this code:

 

<?php 
session_start();
$admin_permission = $_SESSION['admin_permission'];
if(($admin_permission) == 1){
//Session admin ID equal 1
}else{
header('Location: '. $_SERVER['HTTP_HOST'] . '/sign-in.php');
}
include dirname(__FILE__) . '/admin/includes/admin-header.php';
include dirname(__FILE__) . '/admin/includes/admin-navbar.php';
include dirname(__FILE__) . '/admin/includes/admin-functions.php';
include dirname(__FILE__) . '/db/dbconnect.php';
?>


 

 

But this code outputs the errors:

Warning: include(C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-header.php): failed to open stream: No such file or directory in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 9
 
Warning: include(): Failed opening 'C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-header.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 9
 
Warning: include(C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-navbar.php): failed to open stream: No such file or directory in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 10
 
Warning: include(): Failed opening 'C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-navbar.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 10
 
Warning: include(C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-functions.php): failed to open stream: No such file or directory in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 11
 
Warning: include(): Failed opening 'C:\xampp\htdocs\food\admin\crud\customers/admin/includes/admin-functions.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 11
 
Warning: include(C:\xampp\htdocs\food\admin\crud\customers/db/dbconnect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 12
 
Warning: include(): Failed opening 'C:\xampp\htdocs\food\admin\crud\customers/db/dbconnect.php' for inclusion (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\food\admin\crud\customers\add-customers.php on line 12
 


 

 

All I need is to have all my paths starting from the path:

C:\xampp\htdocs\food\, I thought this would help include dirname(__FILE__) but it looks like not....

 

 

Would you have a different solution I could try please?

 

Thank you,

 

Ben

Link to comment
Share on other sites

dirname(__FILE__) will be returning the directory of the current script, for example it will return this file path C:\xampp\htdocs\food\admin\crud\customers

 

You will have to use dirname(dirname(dirname(__FILE__))) to get back to the food directory. The better solution would be to add your admin directory to a constant, then prefix your filepaths using the ADMIN constant, example

define('ADMIN', 'C:/xampp/htdocs/food/admin');
// or as
// define('ADMIN', dirname(dirname(dirname(__FILE__)))

// prefix filepaths with ADMIN constant
include ADMIN . '/includes/admin-header.php';
include ADMIN . '/includes/admin-navbar.php';
include ADMIN . '/admin-functions.php';
include ADMIN . '/db/dbconnect.php';
Link to comment
Share on other sites

The other common tactic is to construct a path relative to the web root:

include $_SERVER['DOCUMENT_ROOT'] . '/food/admin/includes/admin-header.php';
PS: You can use __DIR__ in place of dirname(__FILE__), but you'd still need a couple more dirnames() around it if you tried to use that method. Edited by requinix
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.