Archive

Posts Tagged ‘current’

Directory Iteration With DirectoryIterator() In PHP 5

September 26th, 2008 No comments

The normal way of looping through a directory is to get a handle on the directory, then go through each item, making sure that the file is readable and is not "." or ".." before doing something with the file. Because this is done a lot the DirectoryIterator() object was created in PHP5 to simplify this process.

The constructor of the DirectoryIterator() object takes a single parameter, this is the path of the directory that is to be iterated over.

$dir = new DirectoryIterator('/');

To get the current working directory you can use the PHP function getcwd(). This will return the directory that the PHP script is being run from.

$dir = new DirectoryIterator(getcwd());

The previous bit of code will open up the root directory of your server. If you are on a Linux server you will see directories like dev, var, srv, etc, lib, home, root, sbin and usr.

With the object now created there are some different functions that can be used to look at different things. Here is a small list of what can be used:

  • isDot(): This function is used to see if the file is a "." or a "..".
  • isReadable (): Checks if the file is readable. If it is then it returns true, otherwise it is false.
  • next(): Moves the iterator onto the next item in the list.
  • valid(): This function checks to see if there is anything in the directory that can be iterated over. Basically, this function returns false if the iterator is at the end of the directory.
  • current(): Returns the name of the current file.

These functions can be put together in the following way. This snippet of code will print out all of the files in a directory.

while($dir->valid()){
 if(!$dir->isDot()){
  print $dir->current()."<br />";
 }
 $dir->next();
}

Finding The Current File Or Directory With PHP

May 15th, 2008 No comments

Having a header file that prints out a standard menu on a site is a good idea and saves you time in the long run as you only have to edit one file to change an item on the menu. However, what if you only want to display a menu or sub-menu when a particular page is loaded? This is a common problem, and finding out what page you are on is something that all PHP programmer come across at some point or another.

The PHP $_SERVER superglobal array has three items of interest which can be used to find out the current page. These are PHP_SELF,REQUEST_URI and SCRIPT_NAME and they all appear to have the same values but there are some subtle and important differences. Here are some examples of their values (on the right) with the original URL (on the left).

PHP_SELF
test.php = test.php
/example/ = /example/index.php
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php
/example/test.php/test/123 = /example/test.php/test/123

REQUEST_URI
test.php = test.php
/example/ = /example/
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php?id=123
/example/test.php/test/123 = /example/test.php/test/123

SCRIPT_NAME
test.php = test.php
/example/ = /example/index.php
/example/test.php = /example/test.php
/example/test.php?id=123 = /example/test.php
/example/test.php/test/123 = /example/test.php

So from these tests we can say that SCRIPT_NAME is probably our best bet for getting the filename that is running the code we are looking at. We can get that output like this.

$currentFile = $_SERVER["SCRIPT_NAME"];

Next, you will need to extract the parts of the path into an array and pick out the last item. This will be the filename.

$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = explode('/', $currentFile);
$filename = $parts[count($parts) - 1];

I have seem some variations on this theme, but using SCRIPT_NAME seems to be the most reliable method. One thing to watch out for is that one some server setups you might find that SCRIPT_NAME simply doesn’t exist. In this case I would use PHP_SELF as this is implemented by the language and will always be present.

To find the current directory you only need to look at the rest of the array. So if the file we are looking at has the following URL.

http://www.talkincode.com/example/test/123/test.php

Then our array contained in the $parts variable will contain the following.

[0] =>
[1] => example
[2] => test
[3] => 123
[4] => test.php

The first item is always blank because the SCRIPT_NAME variable always starts with a slash. So when we use the explode() function on it a blank item is created. This can easily be deleted by using the following:

unset($parts[0]);

Categories: PHP Tags: , , , ,

Getting The Current URI In PHP

April 15th, 2008 No comments

The $_SERVER superglobal array contains lots of information about the current page location. You can print this off in full using the following line of code.

echo '<pre>'.print_r($_SERVER,true).'</pre>';

Although this array doesn’t have the full URI we can piece together the current URI using bits of the $_SERVER array. The following function does this and returns a full URI.

function currentUri(){
 $uri = 'http';
 if(isset($_SERVER['HTTPS'])){
  if($_SERVER['HTTPS'] == 'on'){
   $uri .= 's';
  };
 };
 $uri .= '://';
 if($_SERVER['SERVER_PORT'] != '80'){
  $uri .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
 }else{
  $uri .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
 };
 return $uri;
}

You can use this function like this:

echo currentUri();

Categories: PHP Tags: , , , , , ,