Pdksock
Pdksock

Reputation: 1030

php require command error

I have a home.php file which references fileA using php require command which is in the same folder i.e. localhost. the code is:

<?php // /localhost/home.php
require ('/fileA.php');
?>

fileA references another file in its code fileB. so far its workin properly when i access fileC.php i reference fileA using

<?php // /localhost/A/B/fileC.php
require ('../../fileA.php');
?>

the code is able to acquire fileA but only works properly when fileB is in the same folder as fileC else produces a 404 error.I am not able to understand why is this happening since fileB is called by fileA.

thanks.

Upvotes: 0

Views: 139

Answers (1)

dev-null-dweller
dev-null-dweller

Reputation: 29462

When using relative file paths, keep in mind that they are relative to your current working directory, not to file that performs inclusion. current working directory is set automatically on the first request (A/B in your case).

Some simple solutions: always use dirname(__FILE__) before including file, this way it be relative to the file the is in:

<?php 
require(dirname(__FILE__)."/FileA.php"); 
require(dirname(__FILE__)."/../../FileB.php"); 

Upvotes: 2

Related Questions