I’ve always wondered whether relative or absolute file linking within document will have an effect on the code. Will this create conflict with possible inclusion of rewrite rules? And if I were to use absolute file linking. For example, linking /var/pic/hi.jpg using http://www.example.com/var/pic/hi.jpg. Will this cause any additional overhead for the server requesting the linkage or are they identical?
There are no difference for the server.
The request goes to the server would be exactly the same in both cases, /var/pic/hi.jpg
/var/pic/hi.jpg is already absolute path, while http://www.example.com/var/pic/hi.jpg. is not a path at all, but URI
Using http://www.example.com/var/pic/hi.jpg is totally unnecessary and make your code less flexible.
The reason why I asked this is because I’ve thought about using dirname(FILE) to retrieve a base directory which could be used across the server for relative paths. However, I notices that on some of the shared servers this returns a directory which is rooted very deep into the filesystem, and if I were to use this directory to search for my files, it wouldn’t work. That’s why I asked whether using the uri to link has any issues, because instead of using define(“ROOT”, dirname(FILE)) I turned to define(“ROOT”, “http://www.fakewebsite.com”). But this method seems really raw to me and I think there must be a better solution.
Not some of the shared servers, but any server in the world would return path from filesystem root, not the web-server root.
For the last one $_SERVER[‘DOCUMENT_ROOT’] can be used if properly configured
Actually these links from the different words and cannot be substituted by each other.
You can do nothing with files with http://www.fakewebsite.com root
And you’ve always 404 with dirname(FILE) root when trying to link some HTTP resource.
It is very important not to mess filesystem and web-server.
So, for the HTTP links, simple / root is more than enough.
For the filesystem root, both dirname(FILE) or $_SERVER[‘DOCUMENT_ROOT’] can be used.
Yea actually that’s what I meant. I couldn’t remember $_SERVER[‘DOCUMENT_ROOT’] on the spot of typing this up but I’ve tried it also. It would return the same deep level as the other one does. So for example, when I do ROOT.“/images/img.jpg”, the linkage wouldn’t work. So I fetch images and scripts and for anchor links I use define(“ROOT”, “http://www.fakesite.com”). If I use anything else like define(“ROOT”, $_SERVER[‘DOCUMENT_ROOT’]), it doesn’t work. And just using / will cause issues when my call is produced from different directory levels.
You might want to add the path to some variable, and use that variable for your links… In case one day you want to move them to a media server. (change 1 place instead of 1 million places).
I would also let the URLs like: /img/foo.jpg
I’m not sure what you have to gain by putting your domain name in there, and you will type the damn thing until you go insane.
If eventually you want the images loaded off some other server, you can just use an htaccess rule in a front proxy (squid or something), to make the entire /img/ folder load from some other sever.