Working with the File and Operating System with PHP

PHP as a language offers the programmer an excellent array of tools for handling file system input and output, as well as executing programs at the shell level. This article series will introduce you to these tools. It 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).

It’s quite rare to write an application that is entirely self-sufficient–that is, a program that does not rely on at least some level of interaction with external resources, such as the underlying file and operating system, and even other programming languages. The reason for this is simple: As languages, file systems, and operating systems have matured, the opportunities for creating much more efficient, scalable, and timely applications have increased greatly as a result of the developer’s ability to integrate the tried-and-true features of each component into a singular product. Of course, the trick is to choose a language that offers a convenient and efficient means for doing so. Fortunately, PHP satisfies both conditions quite nicely, offering the programmer a wonderful array of tools not only for handling file system input and output, but also for executing programs at the shell level. This chapter serves as an introduction to all such functionality, describing how to work with the following:

  • Files and directories: You’ll learn how to perform file system forensics, revealing details such as file and directory size and location, modification and access times, file pointers (both the hard and symbolic types), and more. 
  • File ownership and permissions: All mainstream operating systems offer a means for securing system data through a permission system based on user and group ownership and rights. You’ll learn how to both identify and manipulate these controls. 
  • File I/O: You’ll learn how to interact with data files, which will let you perform a variety of practical tasks, including creating, deleting, reading, and writing files. 
  • Directory contents: You’ll learn how to easily retrieve directory contents. 
  • Shell commands: You can take advantage of operating system and other language-level functionality from within a PHP application through a number of built-in functions and mechanisms. You’ll learn all about them. This chapter also demonstrates PHPs input sanitization capabilities, showing you how to prevent users from passing data that could potentially cause harm to your data and operating system.

Note  PHP is particularly adept at working with the underlying file system, so much so that it is gaining popularity as a command-line interpreter, a capability introduced in version 4.2.0. Although this topic is out of the scope of this book, you can find additional information in the PHP manual.  

{mospagebreak title=Learning About Files and Directories}

Organizing related data into entities commonly referred to as files and directories has long been a core concept in the computing environment. For this reason, programmers need to have a means for obtaining important details about files and directories, such as the location, size, last modification time, last access time, and other defining information. This section introduces many of PHP’s built-in functions for obtaining these important details.

Parsing Directory Paths

It’s often useful to parse directory paths for various attributes, such as the tailing extension name, directory component, and base name. Several functions are available for performing such tasks, all of which are introduced in this section.


string basename (string path [, string suffix])

The basename() function returns the filename component of path. If the optional suffix parameter is supplied, that suffix will be omitted if the returned file name contains that extension. An example follows:  

$path = "/home/www/data/users.txt";
   $filename = basename($path); // $filename contains "users.txt"
$filename2 = basename($path, ".txt"); // $filename2 contains "users"


string dirname (string path)

The dirname() function is essentially the counterpart to basename(), providing the directory component of path. Reconsidering the previous example:


   $path = "/home/www/data/users.txt";
   $dirname = dirname($path); // $dirname contains "/home/www/data"


array pathinfo (string path)

The pathinfo() function creates an associative array containing three components of the path specified by path: directory name, base name, and extension, referred to by the array keys dirname, basename, and extension, respectively. Consider the following path:


As is relevant to pathinfo(), this path contains three components:

  1. dirname: /home/www/htdocs/book/chapter10 
  2. basename: index.html
  3. extension: html

Therefore, you can use pathinfo() like this to retrieve this information:

   $pathinfo = pathinfo("/home/www/htdocs/book/chapter10/index.html");
   echo "Dir name: $pathinfo[dirname]<br />n";
   echo "Base name: $pathinfo[basename] <br />n";
   echo "Extension: $pathinfo[extension] <br />n";

This returns:

——————————————–Dir name: /home/www/htdocs/book/chapter10 Base name: index.html
Extension: html


string realpath (string path)

The useful realpath() function converts all symbolic links, and relative path references located in path, to their absolute counterparts. For example, suppose your directory structure assumed the following path:


You can use realpath() to resolve any local path references:

     $imgPath = "../../images/cover.gif";
     $absolutePath = realpath($imgPath);
     // Returns /www/htdocs/book/images/cover.gif

Please check back for the second part of this series.

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan