PHP includes a variable in the path

So, we create websites that need to be ported from local development servers, to a test server, and then to a live server. For this reason, we created a variable:

<?php $path = '/_Folder_/_SubFolder_'; ?> 

When we move a website from a server to another, the idea is to simply change the definition of $ path to re-equip a new development server. Currently, on every page, when we call include, we write:

 <?php include('../_includes/_css.php'); ?> 

but I am trying to do this:

 <?php include($path.'/_includes/_css.php'); ?> 

my result I hope this is:

 <?php include('/_Folder_/_SubFolder_/_includes/_css.php'); ?> 

I failed, resulting in:

Warning: include (/ Folder / _SubFolder _ / _ includes / _css.php) [function.include]: could not open the stream: there is no such file or directory in C: \ Program Files \ Apache Software Foundation \ Apache2.2 \ htdocs \ FREEDOM2012_FREEDOM2012_DEFAULT_ \ accommodation \ guest-rooms.php on line 15

Another problem I still need to turn on the variable source by calling "../"

 <?php include('../_includes/_var.php'); ?> 

If anyone finds out how I could do this more efficiently, I would be very grateful. Thank you so much for your time, patience and effort in the response.

+7
source share
2 answers

Why don't you initialize the $ path variable dynamically once and for all and not depending on the environment?

like:

 $path = dirname(__FILE__); 

Starting with PHP 5.3, you also have the __DIR__ shortcut, which does the same as above.

@see: http://www.php.net/manual/en/language.constants.predefined.php

+2
source

You need to show the previous directory with:

 <?php include('../_includes/_css.php'); ?> 

Since you have ../ if your full path is css.php = /_Folder_/_SubFolder_/_includes/_css.php , you need to use this:

 <?php $path = './_Folder_/_SubFolder_'; include($path.'/_includes/_css.php'); ?> 

Otherwise, first specify the full path to the _css.php file.

+2
source

All Articles