Home > Enterprise >  php how to include a file from a different root
php how to include a file from a different root

Time:08-21

For my website www.mysite.com I have a folder structure like this:

root
/login
/includes

In the folder login I have login.php with (simplified):

<?php
  include_once('../includes/includes.php');
?>

If I access www.mysite.com/login/login.php the include works just fine, but if I access login.mysite.com/login.php the include doesn't work.

How can I get this to work?

I've tried using $_SERVER["document_root"] in the include, and I've tried with www.mysite.com/includes/includes.php.

Suggestions are much appreciated.

Edit: Unfortunately I don't have access to php.ini as I am on a web-hotel.

CodePudding user response:

Thanks to @Professor Abronsius: a combination of DIR (magic constant), chdir(), getcwd() and set_include_path() are most useful when setting a path to files for inclusion (either using require or include etc ). You can store a reference to the current working directory with getcwd() for later use if you set different include paths or whatever –

CodePudding user response:

easy way with ssh you can Symlink file to anther locate file

ln -s my_folder/includes.php includes2.php

then use your include

  include_once('includes2.php');
  •  Tags:  
  • php
  • Related