Relative Linking in PHP

This article assumes that you have an Apache / PHP server set up.  If not, I’d suggest something like XAMPP.  It’s downright nifty how, if you grab the zip file, you can start the server and use it right away.

For a basic premise, I’m trying to avoid using absolute links in order to make deployment easier when I copy the website from my development computer onto a production server.

I’m actually under the impression that php maintains a relative linking system based on the file that it’s parsing at the time.

For instance, create two directories and a subdirectory, we’ll name them Test1, Test3, and Test2 respectively.  Lay them out like this:

Test1
–     Test2
Test3

So Test2 is a subdirectory of Test1.  Test3 is its own directory.

Now, inside of Test2, type up the following file named file3.php:

And inside Test3, type up the following file named file1.php:

And inside Test1, type up the following file named file2.php:

So the final layout should be something like:
Test1
–     file2.php
–     Test2
–     file3.php
Test3
–     file1.php

Now, try opening Test3/file1.php.  I believe that your final product should show “Hello World.”

I’m pretty certain that there may be an easier way to maintain a relative linking system, but the interesting part of this method is that you just need to know the entry point.  Which, in the example above, was Test1.

From Test1, I can access all subdirectories relatively.  In other words, you will notice that I did not use the following code in file2.php:

The above works on the theory that the php maintain an absolute link to the file that it’s currently executing (Test2/file1.php) and bases all relative links off of it.  Oddly enough, the above code does work.

But I think it works this way:
Starting from the directory Test1:
– go back a directory
– then open up the directory Test1
– open up the directory Test2
– and then open file3.php.

I wanted the example to mean:
Because we’re executing Test2/file1.php, we start from the directory Test2:
– go back a directory
– then open up the directory Test1
– open up the directory Test2
– and then open file3.php.

Therefore, to test whether my theory is correct, I move file3.php to directory Test2:
Here’s the new code for file2.php:

This is the new layout:
Test1
–     file2.php
–     Test2
Test3
–     file1.php
–     file3.php

If my theory is true, we’ll get an error.  Otherwise, I revise my claim that php maintains a flexible linking system.  I would have to say that it maintains a REALLY flexible linking system.  Because if the above does run without error, it would mean that php maintains at least two ways of resolving relative links.

Huh?  It still runs.

One more test then.
Test1
–     file2.php
–     file3.php
–     Test2
Test3
–     file1.php
–     file3.php

The file3.php under Test1 still contains echo “World”.
The file3.php under Test3 contains echo “World 2

Run…and, we get “Hello World”

Delete the file3.php under Test3

We get “Hello World 2”

So I think it searches for the file from the initial script first.  If it doesn’t find the file, it looks for the file from the path of the currently executing script.

Leave a Reply

Your email address will not be published. Required fields are marked *