Translate Physical Path to VIrtual Path in PHP
<p>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.</p>

<p>Suppose my directory looks like this on my machine:</p>

<p>/abc/php.php</p>

<p>/abc/img/1.png</p>

<p>Now this may be hosted on some server as:</p>

<p>/xyz/php.php</p>

<p>/xyz/img/1.png</p>

<p>I need my <code>&lt;img src="???"&gt;</code> and <code>&lt;form action="???"&gt;</code> such at it works in both cases. That is I need to figure out how to reference <code>1.png</code> from <code>php.php</code> such that it works irrespective of where the site is hosted.</p>

<p>I cannot use something like <code>src="/img/some.png"</code> because that is absolute from <code>DOCUMENT_ROOT</code>. I cannot make any assumption about where relative to the <code>DOCUMENT_ROOT</code> my script is hosted. How should I reference resources in HTML without assuming path from <code>DOCUMENT_ROOT</code>.</p>

<p>Basically, I'm looking for the reverse of <code>realpath</code> and I'm working with pure php not using any frameworks.</p>

<p><strong>PS:</strong> This may sound like a duplicate, but I have spent a better part of the last two hours looking for answers.</p>
