Php-mysql Course

In this tutorial is presented DirectoryIterator, a PHP class added in PHP 5+.
This class is useful to traverse a directory, and get informations about its folders and files (file name, last accessed time, size, etc.).
Like any other class, to use DirectoryIterator you must create an object instance of this class, using:
$objDI = new DirectoryIterator($path);
- $path - represents path of the directory to traverse. For the crrent directory you can use for path: __DIR__ , or dirname(__FILE__).
Then you can use its methods.
- To understand how to use DirectoryIterator , let's see some practical examples.

Get the path to the file

- uses getPath() method.
$objDI = new DirectoryIterator(__DIR__);
echo $objDI->getPath();           // /home/examples/public_html

The instance of DirectoryIterator contains an object with info about the files and directories in the current folder. This object can be traversed with a foreach() instruction to get info of each item.

Get the name of the files in current directory

- Uses isFile() to check if it's a file, and getFilename() to get the name.
$objDI = new DirectoryIterator(__DIR__);
foreach($objDI as $fileinfo) {
  // if it's a file
  if($fileinfo->isFile()) {
    echo $fileinfo->getFilename() .'<br/>';
  }
}

/* Returns something similar to:
 admin.php
 coursesweb.jpg
 jquery.js
 course.html
*/

Get the size of the files

- Uses isFile() to check if it's a file, and getSize() to get the size (in bytes).
$objDI = new DirectoryIterator(__DIR__);
foreach($objDI as $fileinfo) {
  // if it's a file
  if($fileinfo->isFile()) {
    $filesize = $fileinfo->getSize() / 1024;          // 1 KB = 1024 bytes
    echo $fileinfo->getFilename() .' '. $filesize .' KB<br/>';
  }
}

/* Returns something similar to:
 admin.php 3.1572265625 KB
 coursesweb.jpg 11.498046875 KB
 jquery.js 122.095703125 KB
*/

Get directories name

- Uses isDir() to check if it's a directory, isDot() that checks if current item is '.', or '..', and getFilename() to get the name.
$objDI = new DirectoryIterator(__DIR__);
foreach($objDI as $fileinfo) {
  // if it's a directory, but not '.', or '..'
  if($fileinfo->isDir() && !$fileinfo->isDot()) {
    echo $fileinfo->getFilename() .'<br/>';
  }
}

/* Returns something similar to:
 Pages
 CoursesWeb
 Tutorials
*/

Get CHMOD permisions

- Uses isDot() to check if not '.', or '..', and getPerms() to get the permissions of current item (as a decimal integer).
$objDI = new DirectoryIterator(__DIR__);
foreach($objDI as $fileinfo) {
  // if not '.', or '..'
  if(!$fileinfo->isDot()) {
    // get octal from decimal integer
    $octal_perms = substr(sprintf('%o', $fileinfo->getPerms()), -4);
    echo $fileinfo->getFilename() .' '. $octal_perms .'
'; } } /* Returns something similar to: admin.php 0644 Pages 0755 Tutorials.html 0644 */

Check if directory and files are writable

- Uses isWritable(). Returns True if the current file/directory is writable, otherwise, False.
$objDI = new DirectoryIterator(dirname(__FILE__));
foreach($objDI as $fileinfo) {
  if($fileinfo->isWritable()) {
    echo $fileinfo->getFilename() .' is writable<br/>';
  }
}

/* Returns something similar to:
 coursesweb.htm  is writable
 Pages  is writable
 websites.txt  is writable
*/

Get last accessed time of the file

- Uses isFile() to check if it's a file, and getATime() to get the last accessed time, as a Unix timestamp.
$objDI = new DirectoryIterator(dirname(__FILE__));
foreach($objDI as $fileinfo) {
  if($fileinfo->isFile()) {
    echo $fileinfo->getFilename() .' '. $fileinfo->getATime() .'<br/>';
  }
}

/* Returns something similar to:
 coursesweb.htm  1319637336
 Pages  1319880959
 websites.txt 1327757524
*/

• To see the list with all methods of DirectoryIterator class, visit: The DirectoryIterator class.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
DirectoryIterator to get file and directory info

Last accessed pages

  1. The Mastery of Love (7440)
  2. Get Mime Type of file or string content in PHP (6230)
  3. Countdown Timer with starting time added into a form (11533)
  4. Disable button and Enable it after specified time (17533)
  5. JpGraph - Create Graph, Charts, Plots in PHP (3933)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (72)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (64)
  5. CSS3 2D transforms (46)