How Paths Work:
This tutorial is going to explain a bit more about how paths work and what symbols mean within the path. To begin the tutorial, we'll setup a directory tree scenario like you might find on your webhost.
When I login to my site with my favorite ftp client, I see two folders: public_html and private. Files inside the 'private' folder are not viewable over the web, but may be access from scripts running on my website. Files inside 'public_html' are viewable over the web as long as I dont reset their permissions to be restricted.
My main page is named index.html. The url to it will be http://www.daveb.com/index.html. The absolute path to it will be /www/daveb/public_html/index.html. Now, inside my public_html folder, I have my cgi-bin, where I keep all my scripts. Inside the cgi-bin, I like to make seperate folders to keep scripts and their files organized. Today we'll be using a script named 'readfile.pl' inside the 'misc' folder in my cgi-bin. So the url to the script would be http://www.daveb.com/cgi-bin/misc/readfile.pl and the absolute path would be /www/daveb/public_html/cgi-bin/misc/readfile.pl.
My readfile script needs to know the path to a file called 'readme.txt' so it can read it and print the output to a page for me. However, to read from the file, I have to tell it where the file is. This is normally done using an open(FILE, "/path/to/file"); type statement (in YaBB, fopen is used, which is a sub routine written to include both the open command and the file locking procedures all in one). Now my only problem is figuring out the correct path to my file. My file is located in a folder named 'mydocs' which is in the public_html directory. The url to 'mydocs' is http://www.daveb.com/mydocs and the absolute path is /www/daveb/public_html/mydocs.
The easiest way to tell the script where the file is located is to give it the full absolute path to the file. In this case, that is /www/daveb/public_html/mydocs/readme.txt. However, maybe you are too lazy to type all that and you want to use relative paths. Here are some examples of relative paths I could put into the readfile.pl script to have it try and locate the readme.txt file so it can read from it. I'll give examples of several different types of paths, and where the script would be expecting to find the file if it was given each of these paths.
[code]Path Given: 'readme.txt' Url: 'http://www.daveb.com/cgi-bin/misc/readme.txt' Absolute Path: '/www/daveb/public_html/cgi-bin/misc/readme.txt'[/code] Since my readme.txt file is in the 'mydocs' folder, this path is incorrect for my script. If you only give the script the filename, it will look for the file in the same directory that the script is running in).
[code]Path Given: './readme.txt' Url: 'http://www.daveb.com/cgi-bin/misc/readme.txt' Absolute Path: '/www/daveb/public_html/cgi-bin/misc/readme.txt'[/code] This path returns the same result as the one above. './' means current directory. This would also be incorrect for my script.
[code]Path Given: '../readme.txt' Url: 'http://www.daveb.com/cgi-bin/readme.txt' Absolute Path: '/www/daveb/public_html/cgi-bin/readme.txt'[/code] This path is wrong for my script, too. '../' means to move up one directory within the directory heirachy. This tells the script that the file is one directory up from where the script is currently located, which would be the cgi-bin in this case.
[code]Path Given: '/readme.txt' Url: 'N/A' Absolute Path: '/www/daveb/readme.txt'[/code] Once again, this path is wrong for me. By putting only a '/' in front of my filename, the script thinks the file is located in the highest directory in my directory heirarchy. In my case, this is outside of my public webspace. For many people who dont have access to any folders outside of their public webspace with their hosts, '/readme.txt' would mean that the readme.txt was located in their main folder (the first folder you see when you login to your ftp, normally).
[code]Path Given: '../../readme.txt' Url: 'http://www.daveb.com/readme.txt' Absolute Path: '/www/daveb/public_html/readme.txt'[/code] I'm getting closer now, but still wrong. Ive gone up TWO directories from the script this time, which lands me trying to read the file from the public_html folder.
[code]Path Given: '../../mydocs/readme.txt' Url: 'http://www.daveb.com/mydocs/readme.txt' Absolute Path: '/www/daveb/public_html/mydocs/readme.txt'[/code] Ah yes, sweet success. I back out two directories from where the script is located. Now that Im within the public_html directory, I can tell the script to go back into any folders located with public_html. And, what luck, 'mydocs' folder is located there.
[code]Path Given: '/public_html/mydocs/readme.txt' Url: 'http://www.daveb.com/mydocs/readme.txt' Absolute Path: '/www/daveb/public_html/mydocs/readme.txt'[/code] Hey, this works, too. The beginning '/' lets the script know Im starting from the highest directory, and then I just work my way down the path.
Now, lets say I wanted to stick the readme.txt file in the 'private' folder so that it's not accessible over the web. Well, I could do a few paths. See if you can figure them out.
If you said any of these, then you are right: '/www/daveb/private/readme.txt' '/private/readme.txt' '../../../private/readme.txt'
Now here's something to leave you with. Figure out where this path would be looking for the readme.txt file from the readfile.pl script:
./../../cgi-bin/../../private/../public_html/mydocs/../readme.txt
Well, while that's a ridiculous way to go about it, that would put the file at: [code]Url: 'http://www.daveb.com/readme.txt' Absolute Path: '/www/daveb/public_html/readme.txt'[/code]
Hope that helps you in all your path questions.
- DaveB
|