1

I cant seem to make include to work in php

i'm trying to define a function in test.php

and i'm trying to link it to another file out side the test.php file directory what i tried so far is :

include ('../functions.php');  // Doesn't work

error message undefined -- functionname(); in test.php

include ('Doc\somethings\functions.php');  // Doesn't work

What worked for me is putting the whole link to define the functions ! ==>

includes ('F:\JF2\993\Doc\somethings\functions.php') // working but not Ideal ]

I read some devs have the same issue , they told them use this

include ($_SERVER["DOCUMENT_ROOT"] . "../../functions.php");

*Or Try to locate the file path using* 

echo $_SERVER['DOCUMENT_ROOT'];  // [ This Shows the root file path and it's not ideal for me ]

Output

F:\JF2\993\Doc\

am i missing something ?


test.php location

F:\JF2\993\Doc\somethings\scripts\test.php

functions.php location

F:\JF2\993\Doc\somethings\functions.php

additional info :-

Php 7.4

( i need to define a function from a file so i dont have to rewrite it again and again )

10
  • Isn't it just ../functions.php? Commented Oct 18, 2020 at 18:14
  • i added the file file in ../functions.php -- ../../functions.php , it didn't work Commented Oct 18, 2020 at 18:19
  • You are including functions.php inside another file What is the path to functions.php and the path to the file it contained in? Commented Oct 18, 2020 at 18:33
  • F:\JF2\993\Doc\somethings\functions.php Commented Oct 18, 2020 at 18:34
  • the include function is in :: F:\JF2\993\Doc\somethings\scripts\test.php , but the functions commands it's on functions.php Commented Oct 18, 2020 at 18:36

2 Answers 2

1

Php call and include functions

require_once '../functions.php';

include '../functions.php';

require '../functions.php';

header("Location: ../functions.php");

try this

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but still for somereson includes isn't defining the file outside the directory it's only defining the file that on the same directory path
0

My solution is simply define a global that is my base path url, from there on out, i run it like this

if(defined('BASE_PATH')) {

    if(is_file(BASE_PATH.'/core/db/db-config.php')){
    
        require_once (BASE_PATH.'/core/func/system.php');
    
        require_once (BASE_PATH.'/core/db/db.php');
        
        require_once (BASE_PATH.'/core/func/form.php');

    }

}

for the definition of the basepath, i run as first command in my index.php the following

if(!defined('BASE_PATH')) {

    define( 'BASE_PATH',  __DIR__);

}

I do have a fallback that if the defined path is not made (when i go outside of the system to do for instance ajax calls), i add a variable to the session to actually catch that as well.

Why do i do this? Because i prefer to see the full path, and I have no intention on figuring out how many directories i have to step out in order to find what i am looking for. This is also a way to look at it.

Comments

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.