2009/2/18 PJ <af.gour...@videotron.ca>:
> Paul M Foster wrote:
>> On Wed, Feb 18, 2009 at 12:05:21PM -0500, PJ wrote:
>>> Stuart wrote:
>>>> 2009/2/18 PJ <af.gour...@videotron.ca>:
>>>>> Stuart wrote:
>>>>>>>> <?php include dirname(__FILE__)."/../header.php"; ?>
>> <snip>
>>> What confused me here is that often, in examples, there are all sorts of
>>> references to files and there seems to be no standard as to how to refer
>>> to them in non-scripts such as these e-mails. So, I thought that
>>> dirname(_FILE_) was a general reference to a directory name and a
>>> file... :-(
>>> I don't want to defend myself here, but I cannot be expected to know all
>>> functions and look up anything that might resemble a function...
>>> I still do not understand, and that is the keyword here, I am trying to
>>> understand things - what does /../header.php mean. I know the 2 dots
>>> mean a higher directory in Unix... but I understood that ../ would mean
>>> the root directory - so what is the / before the ../header.php mean?
>>> When including scripts or pages, i find that if I am referencing to the
>>> current directory, just the filename or /filename works. If the
>>> reference is up a level, ../ works
>>> e.g. to reference root/images/ from root/authors = ../images/file.ext
>>> from root = /images/file.ext or images/file.ext
>>> I haven't needed to go to a deeper level yet.
>> Let's break it down: dirname(__FILE__) . "/../header.php";
>> __FILE__ is a constant that represents the filename of whatever file
>> it's in. This filename includes the directory to the file.
>> dirname() parses out just the directory for the filename passed as a
>> parameter.
>> The "." is, of course, the "concatenate" parameter for PHP. So we're
>> going to add on whatever comes after the directory for the file.
>> "/../header.php"
>> This one is a little trickier. We want a file called header.php, but
>> it's in a directory just above where you are. In Unix/Linux (and
>> therefore most internet servers), "../header.php" represents a file
>> called header.php in the directory just above where you are. Now, you'll
>> notice that what's quoted is "/../header.php", not "../header.php".
>> There's a leading slash there. Why? That's because we're going to append
>> it to a directory which has no leading slash. So if dirname(__FILE__)
>> yields "/var/www/includes", and you just add "../header.php" to it,
>> you'd get: /var/www/includes../header.php, not the file you want. The
>> file you want is: /var/www/includes/../header.php. And in this case,
>> header.php actually resides in /var/www (one directory up from
>> /var/www/includes).
>> Paul
> OK, great explanation... it's what I had been hoping for ... you're a
> great "educator"
> Thanks...

I agree that Paul's explanation was good, and I apologise for not
doing a better job of explaining it, but it would have taken you 30
seconds to break apart the line I posted and echo out each element
which would have told you exactly what was happening. Teach a man to

For future reference, any token in PHP followed by a ( will be a
function name. And if the token starts with a $ it's a function name
in a variable.

> In my case though, changing from
> Original: include ("lib/header1.php");
> to: include dirname(_FILE_)."/lib/header1.php";
> works, but what have I gained?

You have gained the ability to not care about INI settings. Your
original requires the current directory (.) to be specified as one of
the paths in include_path. In my experience you can never be too
specific or configuration-independent whether you think you're likely
to change server or not.



PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to