Home arrow PHP arrow Page 2 - Executing Shell Commands with PHP

Reading Directory Contents - PHP

In this sixth part of an eight-part series on working with the file and operating systems of a computer with PHP, you'll learn how to use PHP to write to a file and how to execute shell commands. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

TABLE OF CONTENTS:
  1. Executing Shell Commands with PHP
  2. Reading Directory Contents
  3. Executing Shell Commands
By: Apress Publishing
Rating: starstarstarstarstar / 0
December 07, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The process required for reading a directory's contents is quite similar to that involved in reading a file. This section introduces the functions available for this task, and also introduces a function new to PHP 5 that reads a directory's contents into an array.

opendir()

resource opendir (string path)

Just as fopen() opens a file pointer to a given file, opendir() opens a directory stream specified by path.

closedir()

void closedir (resource directory_handle)

The closedir() function closes the directory stream pointed to by directory_handle.

readdir()

string readdir (int directory_handle)

The readdir() function returns each element in the directory specified by directory_handle. You can use this function to list all files and child directories in a given directory:

<?php
$dh = opendir('/usr/local/apache2/htdocs/');
while ($file = readdir($dh))
echo "$file <br>";
closedir($dh);
?>

Sample output follows:

--------------------------------------------.
..
articles
images
news
test.php
--------------------------------------------

Note that readdir() also returns the . and .. entries common to a typical Unix directory listing. You can easily filter these out with an if statement:

if($file != "." AND $file != "..")...

scandir()

array scandir (string directory [,int sorting_order [, resource context]])

The scandir() function, which is new to PHP 5, returns an array consisting of files and directories found in directory, or returns FALSE on error. Setting the optional sorting_order parameter to 1 sorts the contents in descending order, overriding the default of ascending order. Revisiting the example from the previous section:

<?php
print_r(scandir("/usr/local/apache2/htdocs"));
?>

This returns:

--------------------------------------------Array ( [0] => . [1] => .. [2] => articles [3] => images
[4] => news [5] => test.php )
--------------------------------------------

The context parameter refers to a stream context. You'll learn more about this topic in Chapter 16.



 
 
>>> More PHP Articles          >>> More By Apress Publishing
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: