php - Rest api: having trouble with root path directory

I am pretty sure Im writing the directory wrong.
This is the error Im getting on Postman:
Warning: require_once(./includes/functions.php): failed to open stream: No such file or directory in /var/www/html/includes/database.php on line 3
Fatal error: require_once(): Failed opening required './includes/functions.php' (include_path='.:/usr/local/lib/php') in /var/www/html/includes/database.php on line 3
My file structure:
PHP
_api/
__index.php
_includes/
__database.php
__functions.php
My index.php file:
<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
require_once('../includes/database.php');
$db = new operations();
$result=$db->view_record();
?>
The require_once in the database.php file:
<?php
session_start();
require_once('./includes/functions.php');
?>
And in functions.php:
<?php require_once('./includes/database.php');?>
If somebody can help me to find what Im missing on my code I'll appreciate! :)
Answer
Solution:
You are using relative path and becausedatabase.php
andfunctions.php
are in theincludes
folder, you can just remove theincludes
part in the path.
i.e:
database.php
<?php
session_start();
require_once('functions.php');
?>
functions.php
<?php require_once('database.php');?>
Another way to do this is to use absolute path when your include/require (I prefer this way)
i.e:
database.php
<?php
session_start();
require_once(__DIR__ . '/../includes/functions.php');
?>
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: mysqli::real_connect(): (hy000/2002): connection refused
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.