title: PHP: How to get URL of relative file
body: <p>Does PHP have a native function that returns the full URL of a file you declare with a relative path? I need to get: <code>"http://www.domain.com/projects/test/img/share.jpg"</code> from <code>"img/share.jpg"</code> So far I've tried the following:</p>
  
  <pre><code>realpath('img/share.jpg');
  // Returns "/home/user/www.domain.com/projects/test/img/share.jpg"
  </code></pre>
  
  <p>I also tried:</p>
  
  <pre><code>dirname(__FILE__)
  // Returns "/home/user/www.domain.com/projects/test"
  </code></pre>
  
  <p>And <a href="https://stackoverflow.com/questions/6768793/get-the-full-url-in-php">this answer</a> states that the following can be tampered with client-side:</p>
  
  <pre><code>"http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI].'img/share.jpg"
  </code></pre>
  
  <p>plus, my path will vary depending on whether I'm accessing from <code>/test/index.php</code> or just <code>test/</code> without index.php and I don't want to hard-code whether it's http or https.</p>
  
  <p>Does anybody have a solution for this? I'll be sending these files to another person who will upload to their server, so the folder structure will not match <code>"/home/user/www.domain.com/"</code></p>