How to include file outside document root?

前端 未结 7 459
臣服心动
臣服心动 2020-12-03 01:48

What I want do to is to include \'file1.php\' from \'domain1\' into \'file2.php\' on \'domain2\'. So what I figured I should do is something like this:

file2         


        
相关标签:
7条回答
  • 2020-12-03 02:04

    You can include files from anywhere you want, unless your PHP script's permissions, or the safe mode prevent it. Your first approach is perfectly fine. What errors do you get?

    Re the comments, that seem to confirm that there is no access from within PHP to a file that definitely exists. As to what it could be, Suhosin having been ruled out, the only thing I can think of is PHP or Apache being some kind of a a chroot Jail:

    The main benefit of a chroot jail is that the jail will limit the portion of the file system the daemon can see to the root directory of the jail. Additionally, since the jail only needs to support Apache, the programs available in the jail can be extremely limited. Most importantly, there is no need for setuid-root programs, which can be used to gain root access and break out of the jail.

    I have never worked with anything like this so I can't tell you how to spot it (apart from doing a glob() on /var/www/vhosts and see what comes up. But I think this would have to have been set up by an administrator. Who runs your machine?

    0 讨论(0)
  • 2020-12-03 02:05

    I sit here wondering why You didn't jus do a symlink. Or did I mis something? You could symlink a folder with needed includes to the path You have access to.

    0 讨论(0)
  • 2020-12-03 02:07

    What happens if you try to require a different file:

    // test.php
    <?php
       echo 'Hello World';
    ?>
    
    // your file
    require_once('test.php');
    

    Does that work? If so, put test.php in the other location and try it again. Does it still work?

    0 讨论(0)
  • 2020-12-03 02:13

    what you are using is bad practice. put the dependent code into domain specific. Duplicating the same code is the right approach as it does not affect the operation of both sites.

    Try creating the symlink of file1.php and included that as if it is from the local directory.

    also make sure that .htaccess has the followsymlink option set to true

    how about trying this in your file2.php in domain2?

    require_once '../../../domain1/httpdocs/file1.php';

    0 讨论(0)
  • 2020-12-03 02:14

    Try chmod 777 on a test php file to see if that works, if it does you have permission issues. Also do a simple phpinfo() and see if save mode is on.

    0 讨论(0)
  • 2020-12-03 02:26

    You can not accomplish this if open_basedir is in effect, which prevents PHP from traversing out of the home directory.

    What you can do is make sure that docroot1 and docroot2 are owned by users in the same group, set group permissions accordingly and use a symbolic link from docroot2 to docroot1 to read the other web root.

    Or, re-build PHP and let it just follow typical *nix permissions like every other process :)

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