using php __DIR__

后端 未结 2 1981
借酒劲吻你
借酒劲吻你 2021-02-13 10:52

I\'m trying to create a config.php file that defines a global variable which contains the path to a directory called \"projectfiles\". The idea is that any file can point to thi

2条回答
  •  情歌与酒
    2021-02-13 11:51

    It looks like the architecture of this application is less than optimal, but without knowing more, I can't comment on that..

    I suggest defining it as a constant so that it can't be altered and is available no matter the scope.

    define('PROJECTFILES',__DIR__.'/projectfiles/');
    

    Assuming:
    /config.php
    /projectfiles/include_me.php

    include(PROJECTFILES.'include_me.php');
    

    Keep in mind that __DIR__ will reference the directory that config.php is in. If config.php is the parent folder of projectfiles then the above will work.

提交回复
热议问题