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

后端 未结 9 2062
暗喜
暗喜 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: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.

提交回复
热议问题