| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
This file is part of ActiveLink PHP SYS Package (www.active-link.com).
|
|
|
5 |
Copyright (c) 2002-2004 by Zurab Davitiani
|
|
|
6 |
|
|
|
7 |
You can contact the author of this software via E-mail at
|
|
|
8 |
hattrick@mailcan.com
|
|
|
9 |
|
|
|
10 |
ActiveLink PHP SYS Package is free software; you can redistribute it and/or modify
|
|
|
11 |
it under the terms of the GNU Lesser General Public License as published by
|
|
|
12 |
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
13 |
(at your option) any later version.
|
|
|
14 |
|
|
|
15 |
ActiveLink PHP SYS Package is distributed in the hope that it will be useful,
|
|
|
16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
18 |
GNU Lesser General Public License for more details.
|
|
|
19 |
|
|
|
20 |
You should have received a copy of the GNU Lesser General Public License
|
|
|
21 |
along with ActiveLink PHP SYS Package; if not, write to the Free Software
|
|
|
22 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* File class provides a wrapper around filesystem file functions
|
|
|
27 |
* @class File
|
|
|
28 |
* @package org.active-link.sys
|
|
|
29 |
* @author Zurab Davitiani
|
|
|
30 |
* @version 0.4.0
|
|
|
31 |
*/
|
|
|
32 |
|
|
|
33 |
class File {
|
|
|
34 |
|
|
|
35 |
// protected variables
|
|
|
36 |
var $filename;
|
|
|
37 |
var $fileOpenMode;
|
|
|
38 |
var $fileOpenModeRead;
|
|
|
39 |
var $fileOpenModeReadWrite;
|
|
|
40 |
var $fileOpenModeWrite;
|
|
|
41 |
var $fileOpenModeWriteRead;
|
|
|
42 |
var $fileOpenModeAppend;
|
|
|
43 |
var $fileOpenModeAppendRead;
|
|
|
44 |
var $connected;
|
|
|
45 |
var $handleID;
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Constructor accepts filename (optional) and open mode (optional, default "r")
|
|
|
49 |
* If filename is specified, it is opened with the supplied open mode
|
|
|
50 |
* @method File
|
|
|
51 |
* @param optional string filename
|
|
|
52 |
* @param optional string fileOpenMode
|
|
|
53 |
*/
|
|
|
54 |
function File($filename = "", $fileOpenMode = "r") {
|
|
|
55 |
$success = true;
|
|
|
56 |
$this->filename = $filename;
|
|
|
57 |
$this->fileOpenMode = $fileOpenMode;
|
|
|
58 |
$this->fileOpenModeRead = "r";
|
|
|
59 |
$this->fileOpenModeReadWrite = "r+";
|
|
|
60 |
$this->fileOpenModeWrite = "w";
|
|
|
61 |
$this->fileOpenModeWriteRead = "w+";
|
|
|
62 |
$this->fileOpenModeAppend = "a";
|
|
|
63 |
$this->fileOpenModeAppendRead = "a+";
|
|
|
64 |
$this->connected = false;
|
|
|
65 |
$this->handleID = false;
|
|
|
66 |
if($this->filename != "")
|
|
|
67 |
$success = $this->open($this->filename, $this->fileOpenMode);
|
|
|
68 |
return $success;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Closes open file handle, resets filename, and file open mode to defaults
|
|
|
73 |
* @method close
|
|
|
74 |
* @returns true if successful, false otherwise
|
|
|
75 |
*/
|
|
|
76 |
function close() {
|
|
|
77 |
$success = fclose($this->handleID);
|
|
|
78 |
if($success) {
|
|
|
79 |
$this->filename = "";
|
|
|
80 |
$this->fileOpenMode = "r";
|
|
|
81 |
$this->connected = false;
|
|
|
82 |
$this->handleID = false;
|
|
|
83 |
}
|
|
|
84 |
return $success;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* Returns file contents, optionally specify chunk size number of bytes to use per chunk read (default 8192)
|
|
|
89 |
* @method getContents
|
|
|
90 |
* @param optional int chunkSize
|
|
|
91 |
* @returns string file contents if successful, false otherwise
|
|
|
92 |
*/
|
|
|
93 |
function getContents($chunkSize = 8192) {
|
|
|
94 |
if($this->connected) {
|
|
|
95 |
$fileContents = "";
|
|
|
96 |
do {
|
|
|
97 |
$data = fread($this->handleID, $chunkSize);
|
|
|
98 |
if (strlen($data) == 0) {
|
|
|
99 |
break;
|
|
|
100 |
}
|
|
|
101 |
$fileContents .= $data;
|
|
|
102 |
} while(true);
|
|
|
103 |
return $fileContents;
|
|
|
104 |
}
|
|
|
105 |
else
|
|
|
106 |
return false;
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Returns file contents as an array of lines
|
|
|
111 |
* @method getContentsArray
|
|
|
112 |
* @returns array file contents lines
|
|
|
113 |
*/
|
|
|
114 |
function getContentsArray() {
|
|
|
115 |
$fileContentsArray = file($this->filename);
|
|
|
116 |
return $fileContentsArray;
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Opens a file with the supplied open mode
|
|
|
121 |
* @method open
|
|
|
122 |
* @param string filename
|
|
|
123 |
* @param optional string fileOpenMode
|
|
|
124 |
* @returns true if successful, false otherwise
|
|
|
125 |
*/
|
|
|
126 |
function open($filename, $mode = "r") {
|
|
|
127 |
$success = false;
|
|
|
128 |
if(!$this->connected) {
|
|
|
129 |
$this->handleID = @fopen($filename, $mode);
|
|
|
130 |
if($this->handleID !== false) {
|
|
|
131 |
$this->filename = $filename;
|
|
|
132 |
$this->fileOpenMode = $mode;
|
|
|
133 |
$this->connected = true;
|
|
|
134 |
$success = true;
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
return $success;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Writes supplied string content to already open file handle
|
|
|
142 |
* @method write
|
|
|
143 |
* @param string strContent
|
|
|
144 |
* @returns number of bytes written if successful, false otherwise
|
|
|
145 |
*/
|
|
|
146 |
function write($strContent) {
|
|
|
147 |
$bytesWritten = fwrite($this->handleID, $strContent, strlen($strContent));
|
|
|
148 |
return $bytesWritten;
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
?>
|