PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/Ivy_File.php

https://github.com/JamesRandell/Ivy
PHP | 313 lines | 212 code | 59 blank | 42 comment | 57 complexity | 73fb6c0af0d3119f30106a88e366da62 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. <?php
  2. /**
  3. * File module for IVY3.0
  4. *
  5. * @author James Randell
  6. * @version 0.1
  7. * @package File
  8. */
  9. /**
  10. * Creates, updates and retrieves data to/from an XML file.
  11. * @package File
  12. */
  13. class Ivy_File
  14. {
  15. public static $error;
  16. public function __construct ()
  17. {
  18. self::$error = Ivy_Error::getInstance();
  19. }
  20. public $data = array ();
  21. public static function load ($path)
  22. {
  23. if (file_exists($path)) {
  24. return file_get_contents($path);
  25. } else {
  26. self::$error->insert('File does not exist or cannot be read: ' . $path);
  27. print_pre(self::$error->select());
  28. return false;
  29. }
  30. }
  31. public static function readDir ($path)
  32. {
  33. $array = array ();
  34. if (!is_dir($path)) {
  35. return array('error' => 'The directory does not exist');
  36. }
  37. if ($handle = opendir($path)) {
  38. while(($file = readdir($handle)) !== FALSE) {
  39. if ($file == '.' || $file == '..') {
  40. continue;
  41. }
  42. $array[] = $file;
  43. }
  44. }
  45. return $array;
  46. }
  47. /*
  48. public static function select ($path = './')
  49. {
  50. (array) $array = array ();
  51. if (!is_dir($path)) {
  52. if (is_file($path)) {
  53. return file_get_contents($path);
  54. }
  55. return FALSE;
  56. }
  57. if ($handle = opendir($path)) {
  58. while(($file = readdir($handle)) !== FALSE) {
  59. if ($file == '.' || $file == '..') {
  60. continue;
  61. }
  62. $array[] = $file;
  63. }
  64. } else {
  65. return FALSE;
  66. }
  67. return $array;
  68. }
  69. */
  70. public function select ($path = './', $recursive = false)
  71. {
  72. (array) $array = array ();
  73. if (!is_dir($path) && $recursive === false) {
  74. if (is_file($path)) {
  75. return file_get_contents($path);
  76. }
  77. return FALSE;
  78. }
  79. if ($handle = opendir($path)) {
  80. while(($file = readdir($handle)) !== FALSE) {
  81. if ($file == '.' || $file == '..') {
  82. continue;
  83. }
  84. $tokens = explode('/', $path);
  85. $array[$tokens[sizeof($tokens)-2]] = $this->select(rtrim($path, '/') . '/' . $file, true);
  86. //$array[$tokens[sizeof($tokens)-2]][] = $file;
  87. }
  88. } else {
  89. return FALSE;
  90. }
  91. $this->data = $array;
  92. return $array;
  93. }
  94. public static function selectFile ($path = './')
  95. {
  96. (array) $array = array ();
  97. if (!is_dir($path)) {
  98. if (is_file($path)) {
  99. return file_get_contents($path);
  100. }
  101. return FALSE;
  102. }
  103. if ($handle = opendir($path)) {
  104. while(($file = readdir($handle)) !== FALSE) {
  105. if (!is_file($path . '/' . $file) || $file == '.' || $file == '..') {
  106. continue;
  107. }
  108. $array[] = $file;
  109. }
  110. } else {
  111. return FALSE;
  112. }
  113. return $array;
  114. }
  115. /**
  116. * returns a list of folders only
  117. */
  118. public static function selectFolder ($path = './')
  119. {
  120. (array) $array = array ();
  121. if (!is_dir($path)) {
  122. return FALSE;
  123. }
  124. if ($handle = opendir($path)) {
  125. while(($folder = readdir($handle)) !== FALSE) {
  126. if (!is_dir($path . '/' . $folder) || $folder == '.' ||
  127. $folder == '..') {
  128. continue;
  129. }
  130. $array[] = $folder;
  131. }
  132. } else {
  133. return FALSE;
  134. }
  135. return $array;
  136. }
  137. public function delete ($file)
  138. {
  139. return unlink($file);
  140. }
  141. public static function insert ($name, $id)
  142. {
  143. (object) $registry = Ivy_Registry::getInstance();
  144. (string) $string = $registry->data->$id;
  145. if (file_put_contents($name, $string) === FALSE) {
  146. trigger_error('The file could not be written to: ' . $name );
  147. }
  148. return TRUE;
  149. }
  150. public static function copy ($source, $target)
  151. {
  152. if (is_dir($source)) {
  153. mkdir($target);
  154. $d = dir($source);
  155. while (FALSE !== ($entry = $d->read())){
  156. if ($entry == '.' || $entry == '..'){
  157. continue;
  158. }
  159. $Entry = $source .'/'. $entry;
  160. if (is_dir($Entry)) {
  161. Ivy_File::copy($Entry, $target . '/' . $entry);
  162. continue;
  163. }
  164. if (@copy($Entry, $target . '/' . $entry) === FALSE) {
  165. #echo "error copying '$Entry' to '$target/$entry'";echo '<br>';
  166. return FALSE;
  167. }
  168. }
  169. $d->close();
  170. } else {
  171. if (copy($source, $target) === FALSE) {
  172. return FALSE;
  173. }
  174. }
  175. return TRUE;
  176. }
  177. public static function uploadDetail ($array = array ())
  178. {
  179. (array) $array;
  180. if (empty($array)) {
  181. if (isset($_FILES)) {
  182. $array = current($_FILES);
  183. } else {
  184. trigger_error('No $_FILES array found');
  185. return FALSE;
  186. }
  187. }
  188. if (!isset($array['name'])) {
  189. trigger_error('The name of the file was missing when getting file details');
  190. return FALSE;
  191. }
  192. if (empty($array['name'])) {
  193. trigger_error('the name of the file was empty when getting file details');
  194. return FALSE;
  195. }
  196. $nameArray = explode('.', $array['name']);
  197. $result['FILENAME'] = $array['name'];
  198. $result['FILESIZE'] = $array['size'];
  199. $result['FILETYPE'] = $array['type'];
  200. $result['FILEEXT'] = substr(strrchr($array['name'], '.'), 1);
  201. $result['FILETEMPNAME'] = $array['tmp_name'];
  202. return $result;
  203. }
  204. public function upload ($array = array ())
  205. {
  206. (array) $array;
  207. if (!isset($array['PATH'])) {
  208. $array['PATH'] = getcwd() . '/site/' . SITE . '/upload/';
  209. }
  210. if (!isset($array['tmp_name'])) {
  211. $this->error[] = 'tmp_name was missing when trying to upload a file';
  212. return FALSE;
  213. }
  214. if (!isset($array['name'])) {
  215. $this->error[] = 'name was missing when trying to upload a file';
  216. return FALSE;
  217. }
  218. $uploadfile = $array['PATH'] . $array['name'];
  219. if (file_exists($uploadfile)) {
  220. $fileArray = explode('.', $uploadfile);
  221. $extension = $fileArray[ count($fileArray)-1 ];
  222. unset($fileArray[ count($fileArray)-1 ]);
  223. $name = implode('.', $fileArray);
  224. $uploadfile = $name . time() . '.' . $extension;
  225. }
  226. if (move_uploaded_file($array['tmp_name'], $uploadfile)) {
  227. return $uploadfile;
  228. } else {
  229. $this->error[] = 'Upload failed';
  230. return false;
  231. }
  232. }
  233. public function readonly ($file) { $this->readable($file); }
  234. public function readable ($file)
  235. {
  236. if (!is_readable(file)) {
  237. self::$error->insert('File cannot be read: ' . $path);
  238. }
  239. }
  240. public function writeonly ($file) { $this->writeable($file); }
  241. public function writeable ($file)
  242. {
  243. }
  244. public function __destruct ()
  245. {
  246. foreach ($this->error as $key => $value) {
  247. trigger_error($value);
  248. }
  249. }
  250. }
  251. ?>