Search code examples
phphtmlfilepostmandirectory-structure

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! :)


Solution

  • You are using relative path and because database.php and functions.php are in the includes folder, you can just remove the includes 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');
    ?>