I’m returning HTML (which includes IMG SRC attributes) from my php scripts and I cannot make any assumption for the absolute directory structure. I can make relative references.
Suppose my directory looks like this on my machine:
/abc/php.php
/abc/img/1.png
Now this may be hosted on some server as:
/xyz/php.php
/xyz/img/1.png
I need my <img src="???"> and <form action="???"> such at it works in both cases. That is I need to figure out how to reference 1.png from php.php such that it works irrespective of where the site is hosted.
I cannot use something like src="/img/some.png" because that is absolute from DOCUMENT_ROOT. I cannot make any assumption about where relative to the DOCUMENT_ROOT my script is hosted. How should I reference resources in HTML without assuming path from DOCUMENT_ROOT.
Basically, I’m looking for the reverse of realpath and I’m working with pure php not using any frameworks.
PS: This may sound like a duplicate, but I have spent a better part of the last two hours looking for answers.
I usually use the solution used by wordpress in which the absolute path is defined by the user in the configuration section (if such a section exists) or inside a config file.
Then I define a function like this: