5

I have a php file outside my webroot in which I want to include a file that is inside the webroot.

folder outside webroot
- > php file in which I want to include
webroot
- > file to include

So I have to go one directory up, but this doesnt work:

include('../webroot/file-to-include.php');

Include full path doesn't work either:

include('home/xx/xx/domains/mydomain/webroot/file-to-include.php');

How can I accomplish this?

user1662927
  • 193
  • 3
  • 4
  • 9

6 Answers6

12

Full path should be:

include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');

Or you should set the path like:

include(__DIR__ . '/../webroot/file-to-include.php');  // php version >= 5.3
include(dirname(__FILE__) . '/../webroot/file-to-include.php');  // php version < 5.3
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • 2
    The second is a much better alternative. Hardcoding server paths is often a bad idea. – rid Nov 25 '12 at 11:14
  • 1
    Well in that case, the second option worked just as fine, so I'll go with that one – user1662927 Nov 25 '12 at 11:18
  • 1
    @user1662927, think about if you ever need to move the directory. If you hardcode '/xx/xx' everywhere, it's going to be a rather complicated process with the first option. – rid Nov 25 '12 at 11:19
1

Have this in a common file, shared by all your php sources outside the webroot:

<?php

  define('PATH_TO_WEBROOT', '/home/xx/xx/domains/mydomain/webroot');

And then use the following to include files.

<?php
  include (PATH_TO_WEBROOT.'/file-to-include.php');

If the location of your webroot changes, you will only have to change that once in your code base.

You can configure php to automatically prepend a given file to all your scripts, by setting the auto_prepend_file directive. That file could for instance contain the PATH_TO_WEBROOT constant, or require_once the file which contains it. This setting can be done on a per domain or per host basis (see the ini sections documentation).

Also, consider using the autoload feature if you are using classes extensively.

didierc
  • 14,572
  • 3
  • 32
  • 52
0

Try prepending a trailing slash to your full path, so it looks like

include('/home/xx/xx/domains/mydomain/webroot/file-to-include.php');

Otherwise, it will be interpreted as a relative path.

You could also try to change the dir into the webroot and see if this works - for debuggign purposes:

chdir("/home/xx/xx/domains/mydomain/webroot");
include "your_file.php";
David Müller
  • 5,291
  • 2
  • 29
  • 33
0

I put the secured data in the file named conn.txt,

Root file image from FileZilla

and then I used the following PHP command:

$DbInfoFile = "../conn.txt";
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
al_qaysee
  • 81
  • 1
  • 6
0

Fast forward to the current day (2021), an alternative method is to simply add the PHP include_path directive into your php.ini (or user.ini) to point to the includes folder, wherever it is (inside or outside the public root). This method blends the ease of changing one line of code in your project without touching the the PHP code at all. I have no idea if this works on a Windows box since I'm on CentOS.

Example: include_path = ".:/home/{acct-name}/{include-path}" (this would be one level up from public_html or whatever your public folder is called)

This blended approach allows you to simply invoke include 'file-to-include.php'; in your PHP code without going through the whole hassle of defining the root, while retaining the flexibility of changing the includes file by modifying only one line of code sitewide.

By the way, you can have multiple include locations. In that case, you can separate the two locations with a colon such as include_path = ".:/home/{acct-name}/{include-path-1}:/home/{acct-name}/{include-path-2}".

Dark Star
  • 1
  • 1
-6

This should work

$_SERVER['DOCUMENT_ROOT']/home/xx/xx/domains/mydomain/webroot/file-to-include.php

And make sure you have access to that level.

wadie
  • 496
  • 1
  • 10
  • 24
  • `/home` is most likely outside the server's document root. In fact, during the time I worked with web servers, the document root was never `/` (and I can't imagine what an administrator must be thinking to make it `/`). – rid Nov 25 '12 at 11:16