dirname( __FILE__) makes my head hurt. explain how this works please?

Joined
12 Feb 2006
Posts
17,313
Location
Surrey
i've always used include '/p/price-list.php'; etc to include a file but realise the issues this has with being relative to which file calls the include.

i'm reading up and dirname( __FILE__) is better practice but i just can't get my head round it.

so lt's say i'm the file

website.com/admin/index.php

and i've included the file

admin/content/quotes/sort/sort_quote.php

and within that file i'm trying to include a file

admin/p/price-list.php


i can include the first sort_quote.php file, but not the second price-list.php file. everything i try fails.

what should i put?

what is weird is that using xampp and working locally this works. it was only until i uploaded to the server that trying to include the files using my older method failed, and using dirname( __FILE__) never works locally or server

help me ocuk. you're my only hope
 

fez

fez

Caporegime
Joined
22 Aug 2008
Posts
25,796
Location
Tunbridge Wells
It allows you traverse up the file tree. Basically you can call it like dirname(dirname(__FILE__)); you need to go up to a common directory that both files share which in this case would be admin I assume. To avoid doing this some apps either set a global variable which contains the root folder path of the app and then you have a nice base to start from. Other apps might use name spacing to get around this.

Does that make sense? You can nest dirname() as many times as you need to move up through the file tree.
 
Back
Top Bottom