Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


troy beckett
troy beckett
12,035 Points

Understanding paths in php??

I'm new to php and I'm basically trying things out trying to learn. I've now hit a problem which I can't really find an answer to on google.

My problem is to do with absolute and relative paths when using includes/require. I'm pretty sure I understand the difference between absolute and relative paths. However when using them in php they react very differently to what I'm used to in html.

If I type in a relative path which should point to the location of a certain document. I'll get an error like :

-failed to open: no such directory.

No book I have tells me about this and the little things I find on the internet is wrote in a way that is to advance for me.

Firstly what am I missing? Secondly where do I read up on the subject?

4 Answers

the way I organise my files: www - directory and includes - directory are separate. in the includes/settings/config.php (see code attached)


// in every page that was loaded and where you make use of session
// first you have to start the session (open connection on server)

//see the errors for debugging 
ini_set( 'display_errors', 1);
ini_set( 'error_reporting', E_ALL);

//Check the time zone and the date for your area 
// In my case Amsterdam
setlocale(LC_TIME, "nl_NL");

//define the server defaults depends on your host 
// I have default for work space and for home space.
if ( $_SERVER['HTTP_HOST'] == "news_work")
    define( 'SITE_NAME',    'news_work' );
    define( 'ROOT_PATH',    '/Users/Morka/Sites/news.com/www/' );
    define( 'INCLUDE_PATH', '/Users/Morka/Sites/news.com/includes/' );
    define( 'DB_HOST',      'localhost' );
    define( 'DB_USER',      'root' );
    define( 'DB_PASS',      'root' );
    define( 'DB',           'news_site' );

else {
        define( 'SITE_NAME',    'news_home');
        define( 'BASE_URL',     '/');
        define( 'ROOT_PATH',    '/Users/Morka/Sites/news.com/www/' );
        define( 'INCLUDE_PATH', '/Users/Morka/Sites/news.com/includes/' );
        define( 'DB_HOST',      'localhost');
            define( 'DB_USER',      'root');
        define( 'DB_PASS',      'myRoot');
        define( 'DB',       'news_site' );

or just keep it simple...

//index.php The way I include files in my index.php 
basically I use INCLUDE_PATH for the files in the includes folder
and  ROOT_PATH for the files in www folder

// Include the functionality and the config file
// It's the only time that you have to go 1 directory above in order to call the config file
// for the rest of the files we use INCLUDE_PATH or ROOT_PATH
require_once( "../includes/settings/config.php" );

// get the right document
require_once( INCLUDE_PATH ."controllers/newsController.php");
require_once( INCLUDE_PATH ."controllers/userController.php");

// begin with HTML
include_once( INCLUDE_PATH ."views/header.php" );
troy beckett
troy beckett
12,035 Points

Hey, thanks a lot this is really cool. Do you find it's a lot better to define paths the way you do rather than constantly writing relative paths.

What are you using the ROOT_PATH for,? What is the WWW file used for?. Also one more question. Feel free to keep it as short or long as you want.

How did you learn php? and what way would you advise someone else to learn?

Thanks again for your post

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Generally speaking, it means exactly what it says. It can't find the file or directory. However, this is most commonly caused by a typing error. Make sure the file/directory/path is exactly as it's supposed to be. This means paying attention to case as most things are case-sensitive. IE users is not equal to Users.

troy beckett
troy beckett
12,035 Points

Thanks for your comment and I hope your right. If it's just a typing error I'll be very happy.

However I think it's something a bit more than that. There is a lot of posts about it on stackoverflow. But I find it quite hard to understand there answers as they speak as if the person has special knowledge. On treehouse it's usually answered better.

It's think it's something to do with how php views the working directory. Php points to the server, where html points to the url.

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Not necessarily true. You can point html to local directories on your computer. But keep in mind that while path names are not case-sensitive on Windows systems, they are on linux systems which is most likely where the site is being hosted.