Islam Mohamed
Islam Mohamed

Reputation: 91

Include files from parent directory

I'm facing a problem using relative path...I have that directory structure

-dev
 --vendor
   -autoload.php
 --includes
   -index.php

now i need to include autoload.php in index.php As adviced on previous answers i tried include('../vendor/autoload.php'); but it didnt work.

but this didnt work so i had to use this walkaround require_once($_SERVER['DOCUMENT_ROOT'] . '/dev/vendor/autoload.php');

So im wondering why the relative path didnt work and how to make it work?

Upvotes: 0

Views: 505

Answers (1)

Marco
Marco

Reputation: 7261

The only thing that comes to my mind right now is the current working directory.

If you don't specify an absolute path the current working directory will be chosen.

E.g.

<?php
chdir("/tmp");
include "test.php"; // will include /tmp/test.php

And

<?php
chdir("/home/marco");
include "test.php"; // will include /home/marco/test.php

Try changing your path to: __DIR__."/../vendor/autoload.php".

See PHP: Magic Constants for further reference.

Upvotes: 3

Related Questions