Re: Full versus relative URLs

From: Date: Wed, 18 Feb 2009 17:59:16 +0000
Subject: Re: Full versus relative URLs
References: 1 2 3 4 5 6 7 8 9 10 11  Groups: php.general 
Request: Send a blank email to php-general+get-288549@lists.php.net to get a copy of this message
Paul M Foster wrote: > On Wed, Feb 18, 2009 at 12:05:21PM -0500, PJ wrote: > >> Stuart wrote: >>> 2009/2/18 PJ <af.gourmet@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... In my case though, changing from Original: include ("lib/header1.php"); to: include dirname(_FILE_)."/lib/header1.php"; works, but what have I gained? -- Phil Jourdan --- pj@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com

« previous php.general (#288549) next »