Previous Next
RSErrorManager RSStringUtil

RSIO

A Utility Class for IO Operations

Table of Contents

Introduction

RSIO is a static class that provides a few methods that might come in handy when you need to perform some advanced IO operations. It does not try to extend or even replace PEAR::File. PEAR::File is a great package and you are encouraged to use it!

Please note that RSIO::getFileContents does a require_once for RSErrorManager to be able to return an instance of RSError.

RSIO::copyDirectory


1 <?php
2 require_once('RSIO/RSIO.php');
3
4 $source = '/source/directory';
5 $destination = '/destination/directory';
6
7 //we want to overwrite existing files in /destination/directory.
8 //Note: this is the default!
9 $overwrite = true;
10 RSIO::copyDirectory($source, $destination, $overwrite);
11 ?>
Note that the third argument $overwrite is optional. The default is true.

RSIO::replaceInFilename

This method renames a file by doing a string replacement in the filename. It accepts two arguments: the file path and an associative array with search and replace strings. The following peace of code would rename /mypath/tyops_to_be_repalced.txt to /mypath/typos_to_be_replaced.txt.


1 <?php
2 require_once('RSIO/RSIO.php');
3
4 RSIO::replaceInFilename(
5 '/mypath/tyops_to_be_repalced.txt',
6 array(
7 'repalce' => 'replace',
8 'tyop' => 'typo'
9 )
10 );
11 ?>
replaceInFilename will be extended at some time to accept a third (optional) argument: $preg. If it is set to true which will not be the default the search and the replacement string will be treated as pearl regular expressions.

RSIO::replaceInAllFilenames

Renames all files and directories in $path recursively by doing a string replacement. RSIO::replaceInFilename is called for each file and directory to do the actual replacement. This method accepts two arguments: the file path and an associative array with search and replace strings. The following peace of code would rename typos_to_be_repalced1.txt and tyops_to_be_replaced2.txt in /mypath to typos_to_be_replaced1.txt and typos_to_be_replaced2.txt.


1 <?php
2 require_once('RSIO/RSIO.php');
3
4 RSIO::replaceInFilenames(
5 '/mypath',
6 array(
7 'repalce' => 'replace',
8 'tyop' => 'typo'
9 )
10 );
11 ?>
replaceInFilenames will be extended at some time to accept a third (optional) argument: $preg. If it is set to true which will not be the default the search and the replacement string will be treated as pearl regular expressions.

RSIO::getFileContents

Returns the contents of the file $filename or an instance of RSError if fopen failed. This method allows to easyly retrieve the contents of a file without being incompatible to PHP versions lower than 4.3.0 as the native method file_get_contents is. getFileContents uses http://www.php.net/fopen to retrieve a file handle. This method avoids the overhead of PEAR::File and it has the advantage that you do not have to call any rewind method as you have to do in PEAR::File before executing another 'readAll'-operation on the same file. Example:


1 <?php
2 require_once('RSIO/RSIO.php');
3
4 $contents = RSIO::getFileContents('/my_path/my_file.txt');
5 if (PEAR::isError($contents) {
6 //error handling
7 } else {
8 echo $contents;
9 }
10 ?>

Previous Next
RSErrorManager RSStringUtil

Documentation generated on Mon, 8 Dec 2003 13:10:24 +0100 by phpDocumentor 1.2.3