PHP include file in webroot from file outside webroot

后端 未结 5 1345
没有蜡笔的小新
没有蜡笔的小新 2020-12-03 20:02

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

相关标签:
5条回答
  • 2020-12-03 20:13

    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";
    
    0 讨论(0)
  • 2020-12-03 20:14

    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";
    
    0 讨论(0)
  • 2020-12-03 20:14

    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.

    0 讨论(0)
  • 2020-12-03 20:17

    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
    
    0 讨论(0)
  • 2020-12-03 20:25

    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.

    0 讨论(0)
提交回复
热议问题