I have a folder structure as follows:
mydomain.com ->Folder-A ->Folder-B
I have a string from Database that is \'../Folder-B/imag
If you happen to have php 7.0+ you could use levels.
dirname( __FILE__, 2 ) with the second parameter you can define the amount of levels you want to go back.
dirname( __FILE__, 2 )
http://php.net/manual/en/function.dirname.php