PHP Essentials: Going Deeper

Articles > Webmaster > PHP Essentials: Going Deeper

If you read and understood PHP Essentials then you are ready to go a little bit deeper. Consider the following code, based on the example in PHP Essentials:

<html>
  <head><title>My Sites</title></head>
  <body>
  <?php

  if ($_GET["dir"])
    printFolder($_GET["dir"]);
  else
    printFolder();

  function printFolder($folder = ".") {
    /* get all files and folders */
    $files = glob("$folder/*");

    /* print a link back to this folder */
    if (strcmp($folder, ".") != 0) {
      echo "back to ";
      printItem("$folder");
      echo "<br />\n";
    }

    /* print each file or subfolder */
    foreach ($files as $file)
      printItem($file);
  }

  function printItem($x) {
    if (is_dir($x)) {
      $url = "$_SERVER[PHP_SELF]?dir=$x";
      $name = substr(strrchr($x, "/") . "/", 1);
    } else {
      $url = $x;
      $name = substr(strrchr($x, "/"), 1);
    }
    echo "<a href=\"$url\">$name</a><br />\n";
  }
  ?>
  </body>
</html>

This code creates a page that lists the folder items as links, just as before. This time, however, any subfolders are links to another folder listing. So how is this done?

  if ($_GET["dir"])
    printFolder($_GET["dir"]);
  else
    printFolder();

The first thing that you will notice is $_GET. This is a built-in variable1 that contains the values passed on the URL using the GET protocol. In other words, if the client requested the URL http://myhost/myfolder/?dir=mysubfolder then $_GET["dir"] will be "mysubfolder". So what we are doing here is saying "if we were given a folder then use it, otherwise call printFolder() with no arguments.

  function printFolder($folder = ".") {

  }
      

The printFolder function declaration now looks a little different. The optional parameter ($folder = ".") means "if whoever calls this function passes an argument, store that argument in $folder. If they don't pass an argument then store "." in $folder." This means that in the earlier chunk of code, calling printFolder() was the same as calling printFolder(".").

    /* print a link back to this folder */
    if (strcmp($folder, ".") != 0) {
      echo "back to ";
      printItem("$folder");
      echo "<br />\n";
    }
      

 

The strcmp function is a built-in function2 that compares two strings. It returns -1 if the first is alphabetically before the second, 1 if the first is alphabetically after the second, and 0 if the two strings are equal. So our code says "if the folder is not .3 then print a link back to this folder before we list our contents."

    if (is_dir($x)) {
      $url = "$_SERVER[PHP_SELF]?dir=$x";
      $name = substr(strrchr($x, "/") . "/", 1);
    } else {
      $url = $x;
      $name = substr(strrchr($x, "/"), 1);
    }
    echo "<a href=\"$url\">$name</a><br />\n";
      

 

We are using another built-in variable here. $_SERVER is an array of values that contain information about how the script is executing on the server. In particular, $_SERVER["PHP_SELF"] is the filename of the PHP file that is currently being evaluated.

We are also calling two more built-in functions. strrchr returns the portion of a string ending with the given character (i.e., strchr("dir/file.html", "/") will return "/file.html"). substr chops the given amount of the head of a string (i.e., substr("/file.html", 1) will return "file.html").

Notes

  1. See the PHP documentation for a complete list of built-in PHP variables. Return to text.
  2. See the PHP documentation for a complete list of built-in PHP functions. Return to text.
  3. A single period (.) stands for the current working directory. Two periods (..) stands for the parent of the current working directory. Return to text.