How do I include a file over 2 directories back?

后端 未结 19 1457
遥遥无期
遥遥无期 2020-12-22 17:51

How do you include a file that is more than 2 directories back. I know you can use ../index.php to include a file that is 2 directories back, but how do you do

相关标签:
19条回答
  • 2020-12-22 18:15

    But be VERY careful about letting a user select the file. You don't really want to allow them to get a file called, for example,

    ../../../../../../../../../../etc/passwd
    

    or other sensitive system files.

    (Sorry, it's been a while since I was a linux sysadmin, and I think this is a sensitive file, from what I remember)

    0 讨论(0)
  • 2020-12-22 18:17

    .. selects the parent directory from the current. Of course, this can be chained:

    ../../index.php
    

    This would be two directories up.

    0 讨论(0)
  • 2020-12-22 18:18

    if you are using php7 you can use dirname function with level parameter of 2, for example :

    dirname("/usr/local/lib", 2);
    

    the second parameter "2" indicate how many level up

    dirname referance

    0 讨论(0)
  • 2020-12-22 18:18
    ../../../includes/boot.inc.php
    
    0 讨论(0)
  • 2020-12-22 18:19

    You can do ../../directory/file.txt - This goes two directories back.

    ../../../ - this goes three. etc

    0 讨论(0)
  • 2020-12-22 18:21

    ../ is one directory, Repeat for two directories ../../ or even three: ../../../ and so on.

    Defining constants may reduce confusion because you will drill forward into directories verses backwards

    You could define some constants like so:

    define('BD', '/home/user/public_html/example/');
    
    define('HTMLBD', 'http://example.com/');
    

    When using 'BD' or my 'base directory' it looks like so:

    file(BD.'location/of/file.php');
    

    define(); reference

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