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
https://forums.phpfreaks.com/topic/297390-php-path-problem-with-include-files/
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';

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.
  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.