Source for file RSIO.php

Documentation is available at RSIO.php


1 <?php
2 // RSIO: A Utility Class for IO Operations
3 // Copyright (C) 2003 Lukas Feiler
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 /**This file contains the class RSIO.
20 *
21 * @package RSIO
22 * @author Lukas Feiler <lukas.feiler@chello.at>
23 * @copyright Lukas Feiler 2003
24 * @filesource
25 */
26
27 /**To be passed to some of the methods of RSIO if you wish overwriting.
28 */
29 DEFINE('RSIO_OVERWRITE',true);
30
31 /**To be passed to some of the methods of RSIO if you not wish overwriting.
32 */
33 DEFINE('RSIO_NO_OVERWRITE',false);
34
35 /**The error code that will be returned if getFileContents fails.
36 */
37 DEFINE('RSIO_ERROR_CANNOT_READ_FILE', -1001);
38
39 /**A Utility Class for IO Operations.
40 *
41 * This class provides methods not included in PEARS's IO packages.
42 *
43 * @author Lukas Feiler <lukas.feiler@chello.at>
44 * @version 0.1.9
45 * @copyright Lukas Feiler 2003
46 * @package RSIO
47 * @static
48 */
49 class RSIO {
50
51 /**Copies a directory including all subdirectories and files.
52 *
53 * @param String $from_path Source: the directory to copy. MUST end with a slash.
54 * @param String $to_path Destination: where the direcotry specified by the first argument should be copied to. MUST end with a slash.
55 */
56 function copyDirectory($from_path, $to_path, $overwrite = true)
57 {
58 $this_path = getcwd();
59 if (!is_dir($to_path)) {
60 mkdir($to_path, 0775);
61 }
62 if (is_dir($from_path)) {
63 chdir($from_path);
64 $handle=opendir('.');
65 while (($file = readdir($handle))!==false) {
66 if (($file != ".") && ($file != "..")) {
67 if (is_dir($file)) {
68 chdir($this_path);
69 RSIO::copyDirectory($from_path.$file."/", $to_path.$file."/", $overwrite);
70 chdir($this_path);
71 chdir($from_path);
72 }
73 if (is_file($file)) {
74 chdir($this_path);
75 if (!file_exists($to_path.$file) || $overwrite === true) {
76 copy($from_path.$file, $to_path.$file);
77 }
78 chdir($from_path);
79 }
80 }
81 }
82 closedir($handle);
83 }
84 chdir($this_path);
85 }
86
87 /**Renames a file by doing a string replacement.
88 *
89 * Example:
90 * <code>
91 * RSIO::replaceInFilename(
92 * '/mypath/tyops_to_be_repalced.txt',
93 * array(
94 * 'repalce' => 'replace',
95 * 'tyop' => 'typo'
96 * )
97 * );
98 * </code>
99 * This would rename /mypath/tyops_to_be_repalced.txt to /mypath/typos_to_be_replaced.txt.
100 *
101 * @param String $path The path to the file.
102 * @param Array $replacements An associative array of search and replacement strings
103 * as key-value pairs.
104 * @return boolean True on success, false on failure. Note that if the filename does not contain
105 * one of the search strings the file is not renamed and true is returned.
106 */
107 function replaceInFilename($path, $replacements)
108 {
109 if (!file_exists($path)) {
110 return false;
111 }
112 $filename = basename($path);
113
114 //do multiple replacement defined by an associativ array passed as second argument.
115 while (list($searchStr, $replaceStr) = each($replacements)) {
116 $filename = str_replace($searchStr, $replaceStr, $filename);
117 }
118
119 //if any replacement occured
120 if ($filename != basename($path)) {
121 return rename($path, dirname($path) . "/" . $filename);
122 }
123 return true;
124 }
125
126 /**Renames all files and directories in $path recursively by doing a string replacement.
127 *
128 * RSIO::replaceInFilename is called for each file and directory to do the actual replacement.
129 * See RSIO::replaceInFilename for details.
130 * @see replaceInFilename
131 *
132 * @param String $path The path to the directory where to start the operation.
133 * @param Array $replacements An associative array of search and replacement strings
134 * as key-value pairs.
135 */
136 function replaceInAllFilenames($path, $replacements)
137 {
138 $cwd = getcwd();
139 if (is_dir($path)) {
140 chdir($path);
141 $handle=opendir('.');
142 while (($file = readdir($handle))!==false) {
143 if (($file != ".") && ($file != "..")) {
144 if (is_dir($file)) {
145 RSIO::replaceInAllFilenames("$path/$file", $replacements);
146 }
147 RSIO::replaceInFilename("$path/$file", $replacements);
148 }
149 }
150 closedir($handle);
151 }
152 chdir($cwd);
153 }
154
155 /**Returns the contents of the file $filename or an instance of RSError if fopen failed.
156 *
157 * This method allows to easyly retrieve the contents of a file without being incompatible
158 * to PHP versions < 4.3.0 as the native method file_get_contents is. getFileContents uses
159 * {@link PHP_MANUAL#fopen} to retrieve a file handle.
160 * This method avoids the overhead of PEAR::File and it has the advantage that you do not
161 * have to call any rewind method as you have to do in PEAR::File before executing another
162 * 'readAll'-operation on the same file.
163 * Example:
164 * <code>
165 * $contents = RSIO::getFileContents('/my_path/my_file.txt');
166 * if (PEAR::isError($contents) {
167 * //error handling
168 * } else {
169 * echo $contents;
170 * }
171 * </code>
172 *
173 * @param String $filename A filename valid for fopen
174 */
175 function getFileContents($filename)
176 {
177 $handle = @fopen($filename, "r");
178 if ($handle === false) {
179
180 /**RSErrorManager is loaded on demand.
181 */
182 require_once('RSErrorManager/RSErrorManager.php');
183
184 return RSErrorManager::raiseRSError('RSIO', $filename, 'getFileContents', "Cannot open $filename.", RSIO_ERROR_CANNOT_READ_FILE);
185 }
186 $contents = fread($handle, filesize($filename));
187 fclose ($handle);
188 return $contents;
189 }
190 }

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