I understand what he's saying. I implemented the same kind of check in my downloads script, so people couldn't escape out of the 'downloads' directory and into my root.
Try looking at [man]dirname/man and [man]realpath/man
EDIT: Also, look into using Safe Mode with open_basedir restrictions.