Page Index Toggle Pages: 1 Print
Understanding Paths (Read 3215 times)
Captain John

Understanding Paths
Sep 24th, 2007 at 11:58pm
Print Post  
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 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 '' inside the 'misc' folder in my cgi-bin. So the url to the script would be and the absolute path would be /www/daveb/public_html/cgi-bin/misc/ 

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 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 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.

Path Given: 'readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/cgi-bin/misc/readme.txt'

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).

Path Given: './readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/cgi-bin/misc/readme.txt'

This path returns the same result as the one above. './' means current directory. This would also be incorrect for my script.

Path Given: '../readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/cgi-bin/readme.txt'

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.

Path Given: '/readme.txt'
Url: 'N/A'
Absolute Path: '/www/daveb/readme.txt'

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).

Path Given: '../../readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/readme.txt'

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.

Path Given: '../../mydocs/readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/mydocs/readme.txt'

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.

Path Given: '/public_html/mydocs/readme.txt'
Url: ''
Absolute Path: '/www/daveb/public_html/mydocs/readme.txt'

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:

Now here's something to leave you with. Figure out where this path would be looking for the readme.txt file from the script:


Well, while that's a ridiculous way to go about it, that would put the file at:

Select All
Url: ''
Absolute Path: '/www/daveb/public_html/readme.txt' 

Reference David Baughman on;action=display;num=99861466...
Back to top
IP Logged
Page Index Toggle Pages: 1
  « Board Index ‹ Board  ^Top