file.php
Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 if (!class_exists('Object')) {
00032 uses ('object');
00033 }
00034
00035 if (!class_exists('Folder')) {
00036 uses('folder');
00037 }
00038
00039
00040
00041
00042
00043
00044 class File extends Object {
00045
00046
00047
00048
00049
00050
00051 var $Folder = null;
00052
00053
00054
00055
00056
00057
00058 var $name = null;
00059
00060
00061
00062
00063
00064
00065 var $info = array();
00066
00067
00068
00069
00070
00071
00072 var $handle = null;
00073
00074
00075
00076
00077
00078
00079 var $lock = null;
00080
00081
00082
00083
00084
00085
00086
00087
00088 function __construct($path, $create = false, $mode = 0755) {
00089 parent::__construct();
00090 $this->Folder =& new Folder(dirname($path), $create, $mode);
00091 if (!is_dir($path)) {
00092 $this->name = basename($path);
00093 }
00094
00095 if (!$this->exists()) {
00096 if ($create === true) {
00097 if ($this->safe($path) && $this->create() === false) {
00098 return false;
00099 }
00100 } else {
00101 return false;
00102 }
00103 }
00104 }
00105
00106
00107
00108
00109
00110 function __destruct() {
00111 $this->close();
00112 }
00113
00114
00115
00116
00117
00118
00119 function create() {
00120 $dir = $this->Folder->pwd();
00121 if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
00122 if (touch($this->pwd())) {
00123 return true;
00124 }
00125 }
00126 return false;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136 function open($mode = 'r', $force = false) {
00137 if (!$force && is_resource($this->handle)) {
00138 return true;
00139 }
00140 if ($this->exists() === false) {
00141 if ($this->create() === false) {
00142 return false;
00143 }
00144 }
00145
00146 $this->handle = fopen($this->pwd(), $mode);
00147 if (is_resource($this->handle)) {
00148 return true;
00149 }
00150 return false;
00151 }
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161 function read($bytes = false, $mode = 'rb', $force = false) {
00162 $success = false;
00163 if ($this->lock !== null) {
00164 if (flock($this->handle, LOCK_SH) === false) {
00165 return false;
00166 }
00167 }
00168 if ($bytes === false) {
00169 $success = file_get_contents($this->pwd());
00170 } elseif ($this->open($mode, $force) === true) {
00171 if (is_int($bytes)) {
00172 $success = fread($this->handle, $bytes);
00173 } else {
00174 $data = '';
00175 while (!feof($this->handle)) {
00176 $data .= fgets($this->handle, 4096);
00177 }
00178 $success = trim($data);
00179 }
00180 }
00181 if ($this->lock !== null) {
00182 flock($this->handle, LOCK_UN);
00183 }
00184 return $success;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194 function offset($offset = false, $seek = SEEK_SET) {
00195 if ($offset === false) {
00196 if (is_resource($this->handle)) {
00197 return ftell($this->handle);
00198 }
00199 } elseif ($this->open() === true) {
00200 return fseek($this->handle, $offset, $seek) === 0;
00201 }
00202 return false;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212 function prepare($data, $forceWindows = false) {
00213 $lineBreak = "\n";
00214 if (substr(PHP_OS,0,3) == "WIN" || $forceWindows === true) {
00215 $lineBreak = "\r\n";
00216 }
00217 return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
00218 }
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229 function write($data, $mode = 'w', $force = false) {
00230 $success = false;
00231 if ($this->open($mode, $force) === true) {
00232 if($this->lock !== null) {
00233 if(flock($this->handle, LOCK_EX) === false) {
00234 return false;
00235 }
00236 }
00237
00238 if (fwrite($this->handle, $data) !== false) {
00239 $success = true;
00240 }
00241 if ($this->lock !== null) {
00242 flock($this->handle, LOCK_UN);
00243 }
00244 }
00245 return $success;
00246 }
00247
00248
00249
00250
00251
00252
00253
00254
00255 function append($data, $force = false) {
00256 return $this->write($data, 'a', $force);
00257 }
00258
00259
00260
00261
00262
00263
00264 function close() {
00265 if (!is_resource($this->handle)) {
00266 return true;
00267 }
00268 return fclose($this->handle);
00269 }
00270
00271
00272
00273
00274
00275
00276 function delete() {
00277 if ($this->exists()) {
00278 return unlink($this->pwd());
00279 }
00280 return false;
00281 }
00282
00283
00284
00285
00286
00287
00288 function info() {
00289 if ($this->info == null) {
00290 $this->info = pathinfo($this->pwd());
00291 }
00292 if (!isset($this->info['filename'])) {
00293 $this->info['filename'] = $this->name();
00294 }
00295 return $this->info;
00296 }
00297
00298
00299
00300
00301
00302
00303 function ext() {
00304 if ($this->info == null) {
00305 $this->info();
00306 }
00307 if (isset($this->info['extension'])) {
00308 return $this->info['extension'];
00309 }
00310 return false;
00311 }
00312
00313
00314
00315
00316
00317
00318 function name() {
00319 if ($this->info == null) {
00320 $this->info();
00321 }
00322 if (isset($this->info['extension'])) {
00323 return basename($this->name, '.'.$this->info['extension']);
00324 } elseif ($this->name) {
00325 return $this->name;
00326 }
00327 return false;
00328 }
00329
00330
00331
00332
00333
00334
00335
00336 function safe($name = null, $ext = null) {
00337 if (!$name) {
00338 $name = $this->name;
00339 }
00340 if (!$ext) {
00341 $ext = $this->ext();
00342 }
00343 return preg_replace( "/[^\w\.-]+/", "_", basename($name, $ext));
00344 }
00345
00346
00347
00348
00349
00350
00351
00352 function md5($maxsize = 5) {
00353 if ($maxsize === true) {
00354 return md5_file($this->pwd());
00355 } else {
00356 $size = $this->size();
00357 if ($size && $size < ($maxsize * 1024) * 1024) {
00358 return md5_file($this->pwd());
00359 }
00360 }
00361 return false;
00362 }
00363
00364
00365
00366
00367
00368
00369 function pwd() {
00370 return $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
00371 }
00372
00373
00374
00375
00376
00377
00378 function exists() {
00379 $exists = (file_exists($this->pwd()) && is_file($this->pwd()));
00380 return $exists;
00381 }
00382
00383
00384
00385
00386
00387
00388 function perms() {
00389 if ($this->exists()) {
00390 return substr(sprintf('%o', fileperms($this->pwd())), -4);
00391 }
00392 return false;
00393 }
00394
00395
00396
00397
00398
00399
00400
00401 function size() {
00402 if ($this->exists()) {
00403 return filesize($this->pwd());
00404 }
00405 return false;
00406 }
00407
00408
00409
00410
00411
00412
00413 function writable() {
00414 return is_writable($this->pwd());
00415 }
00416
00417
00418
00419
00420
00421
00422 function executable() {
00423 return is_executable($this->pwd());
00424 }
00425
00426
00427
00428
00429
00430
00431 function readable() {
00432 return is_readable($this->pwd());
00433 }
00434
00435
00436
00437
00438
00439 function owner() {
00440 if ($this->exists()) {
00441 return fileowner($this->pwd());
00442 }
00443 return false;
00444 }
00445
00446
00447
00448
00449
00450
00451 function group() {
00452 if ($this->exists()) {
00453 return filegroup($this->pwd());
00454 }
00455 return false;
00456 }
00457
00458
00459
00460
00461
00462
00463 function lastAccess() {
00464 if ($this->exists()) {
00465 return fileatime($this->pwd());
00466 }
00467 return false;
00468 }
00469
00470
00471
00472
00473
00474
00475 function lastChange() {
00476 if ($this->exists()) {
00477 return filemtime($this->pwd());
00478 }
00479 return false;
00480 }
00481
00482
00483
00484
00485
00486
00487 function &Folder() {
00488 return $this->Folder;
00489 }
00490 }
00491 ?>