php how to go one level up on dirname(__FILE__)

后端 未结 9 2048
暗喜
暗喜 2020-11-30 21:52

I have a folder structure as follows:

mydomain.com
  ->Folder-A
  ->Folder-B

I have a string from Database that is \'../Folder-B/imag

相关标签:
9条回答
  • 2020-11-30 22:29

    One level up, I have used:

    str_replace(basename(__DIR__) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
    

    or for php < 5.3:

    str_replace(basename(dirname(__FILE__)) . '/' . basename(__FILE__), '', realpath(__FILE__)) . '/required.php';
    
    0 讨论(0)
  • 2020-11-30 22:31

    Try this

    dirname(dirname( __ FILE__))
    

    Edit: removed "./" because it isn't correct syntax. Without it, it works perfectly.

    0 讨论(0)
  • 2020-11-30 22:34

    You can use realpath to remove unnessesary part:

    // One level up
    echo str_replace(realpath(dirname(__FILE__) . '/..'), '', realpath(dirname(__FILE__)));
    
    // Two levels etc.
    echo str_replace(realpath(dirname(__FILE__) . '/../..'), '', realpath(dirname(__FILE__)));
    

    On windows also replace \ with / if need that in URL.

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