Home arrow PHP arrow Page 2 - Working with the File and Operating System with PHP

Learning About Files and Directories - 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).

TABLE OF CONTENTS:
  1. Working with the File and Operating System with PHP
  2. Learning About Files and Directories
By: Apress Publishing
Rating: starstarstarstarstar / 0
November 04, 2011

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.

basename()

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:  

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

dirname()

string dirname (string path)

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

<?php 

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

pathinfo()

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:

/home/www/htdocs/book/chapter10/index.html

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:

<?php
   $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
--------------------------------------------

realpath()

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:

/home/www/htdocs/book/images/

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

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

Please check back for the second part of this series.



 
 
>>> 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: