Kolja
Kolja

Reputation: 868

include fails if file is in another directory

I am trying to include a php file that has some functions in it. My original plan was to store it in a different directory, but when I do that, include fails.

Here is what I mean:

var_dump(file_exists("/dir/functions.php")); // returns bool(false)
var_dump(file_exists("dir/functions.php")); // returns bool(false)
var_dump(file_exists("http://www.example.com/dir/functions.php")); // also returns bool(false)
var_dump(file_exists("functions.php")); // returns bool(true) if I place the file in the same directory
var_dump(include("/dir/functions.php")); // returns bool(false)
var_dump(include("dir/functions.php")); // returns bool(false)
var_dump(function_exists(someFunction));// returns bool(false)

var_dump(include("functions.php"));// returns bool(true) if I place the file in the same directory
var_dump(function_exists(someFunction));// returns bool(true)

What am I doing wrong? I checked and the files are all there, and they have read and execute permission, i.e. 755

Upvotes: 0

Views: 64

Answers (2)

user1231342435346354
user1231342435346354

Reputation: 47

What is the location of this(your example) file? Maybe you need to do ../ or go back to root location and then making the path.

Example:

include('../dir/script.php');

Upvotes: 0

splash58
splash58

Reputation: 26143

function include requires a full server path or a path relative to the directory where the calling script is located Since the directory structure

www.example.com/site/index.php
www.example.com/dir/functions.php

the correct call with a relative path is

include("../dir/functions.php");

Upvotes: 1

Related Questions