I have a folder structure as follows:
mydomain.com
->Folder-A
->Folder-B
I have a string from Database that is \'../Folder-B/imag
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';
Try this
dirname(dirname( __ FILE__))
Edit: removed "./" because it isn't correct syntax. Without it, it works perfectly.
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.