You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
4.1 KiB

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