22

I have a variable on my site called $basePath which is set as:

$basePath = '/Systems/dgw/';

I am using it on all my css, js and images tags as so (shortened for better visibility):

<link href="<?php echo $basePath; ?>include/assets/css/bootstrap.min.css">

I have no problem with those includes and they work fine in wherever file and in whatever folder I am.

I have a certain included page which has the following line:

<img src="<?php echo $basePath; ?>images/new_logo.png" alt="logo"/>

And the image shows just fine. The line after it states:

<?php include($basePath.'include/assets/common/topMessages.php');?>

But the include doesn't happens. When I try it like this:

<?php include('../../include/assets/common/topMessages.php');?>

It works.

Anybody has any idea what could be wrong?

3
  • echo does not include anything …
    – CBroe
    Apr 7, 2014 at 12:16
  • Sorry, Copied the wrong line. Fixed.
    – digicom
    Apr 7, 2014 at 12:18
  • 6
    Basically there's a distinction between your webroot (ie. www.example.com/ is the root of your website) and the root of a (unix) filesystem. The webroot is mapped to a path in the filesystem; often www.example.com/ is mapped to something like /var/www/example.com/http_docs/. Your webserver automatically translates any requests for /a/b/c to /var/www/example.com/http_docs/a/b/c. However when you include a file via PHP, the webserver is not asked to translate the path, and as such it looks for /a/b/c in your filesystem, which obviously does not exist.
    – Tularis
    Apr 7, 2014 at 12:23

3 Answers 3

69

You can't include php files relatively to your webroot that way, cause if you use the slash as first character, the reference will go much deeper than just your document root. So, instead of using your basepath, you could do something like this :

<?php 
   $path = $_SERVER['DOCUMENT_ROOT'];
   $path .= "/yourpath/yourfile.php";
   include_once($path);
?>
0
4

If your server doesn't populate the "document_root", you may need this



    require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/path/to/file.php");

I use this line of code. It goes back to the "top" of the site tree, then goes to the file desired.
For example, let's say i have this file tree:
domain.com/aaa/index.php
domain.com/bbb/ccc/ddd/index.php
domain.com/_resources/functions.php

I can include the functions.php file from wherever i am, just by copy pasting

require(str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))."/_resources/functions.php");



If you need to use this code many times, you may create a function that returns the "str_repeat('../',(substr_count(getenv('SCRIPT_URL'),'/')-1))" part. Then just insert this function in the first file you include. I have an "initialize.php" file that i include at the very top of each php page and which contains this function. The next time i have to include files, i in fact just use the function (named "path_back"):

require(path_back()."/_resources/another_php_file.php");
1
  • Good solution! But fails for files at root level. Jan 23, 2019 at 11:18
1

You can add an include_path = ".:/home/myuser/mysite.com/" to your php.ini or you can add something like this into your script before the include or require:

set_include_path(get_include_path() . ":/home/myuser/mysite.com/");

The first one will work for all the scripts running in your website.

The second option will only work for the script which has the setincludepath on the code, for the rest of the application it will not work unless you have an object you call in every script that add the setincludepath.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.