/convert/system/helpers/file_helper.php

https://github.com/usagi-project/mynets1 · PHP · 192 lines · 71 code · 20 blank · 101 comment · 17 complexity · b6c815533e62e6807e64fae8a6c787e2 MD5 · raw file

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * CodeIgniter File Helpers
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Helpers
  21. * @category Helpers
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/helpers/file_helpers.html
  24. */
  25. // ------------------------------------------------------------------------
  26. /**
  27. * Read File
  28. *
  29. * Opens the file specfied in the path and returns it as a string.
  30. *
  31. * @access public
  32. * @param string path to file
  33. * @return string
  34. */
  35. if (! function_exists('read_file'))
  36. {
  37. function read_file($file)
  38. {
  39. if ( ! file_exists($file))
  40. {
  41. return FALSE;
  42. }
  43. if (function_exists('file_get_contents'))
  44. {
  45. return file_get_contents($file);
  46. }
  47. if ( ! $fp = @fopen($file, 'rb'))
  48. {
  49. return FALSE;
  50. }
  51. flock($fp, LOCK_SH);
  52. $data = '';
  53. if (filesize($file) > 0)
  54. {
  55. $data =& fread($fp, filesize($file));
  56. }
  57. flock($fp, LOCK_UN);
  58. fclose($fp);
  59. return $data;
  60. }
  61. }
  62. // ------------------------------------------------------------------------
  63. /**
  64. * Write File
  65. *
  66. * Writes data to the file specified in the path.
  67. * Creates a new file if non-existent.
  68. *
  69. * @access public
  70. * @param string path to file
  71. * @param string file data
  72. * @return bool
  73. */
  74. if (! function_exists('write_file'))
  75. {
  76. function write_file($path, $data, $mode = 'wb')
  77. {
  78. if ( ! $fp = @fopen($path, $mode))
  79. {
  80. return FALSE;
  81. }
  82. flock($fp, LOCK_EX);
  83. fwrite($fp, $data);
  84. flock($fp, LOCK_UN);
  85. fclose($fp);
  86. return TRUE;
  87. }
  88. }
  89. // ------------------------------------------------------------------------
  90. /**
  91. * Delete Files
  92. *
  93. * Deletes all files contained in the supplied directory path.
  94. * Files must be writable or owned by the system in order to be deleted.
  95. * If the second parameter is set to TRUE, any directories contained
  96. * within the supplied base directory will be nuked as well.
  97. *
  98. * @access public
  99. * @param string path to file
  100. * @param bool whether to delete any directories found in the path
  101. * @return bool
  102. */
  103. if (! function_exists('delete_files'))
  104. {
  105. function delete_files($path, $del_dir = FALSE, $level = 0)
  106. {
  107. // Trim the trailing slash
  108. $path = preg_replace("|^(.+?)/*$|", "\\1", $path);
  109. if ( ! $current_dir = @opendir($path))
  110. return;
  111. while(FALSE !== ($filename = @readdir($current_dir)))
  112. {
  113. if ($filename != "." and $filename != "..")
  114. {
  115. if (is_dir($path.'/'.$filename))
  116. {
  117. delete_files($path.'/'.$filename, $del_dir, $level + 1);
  118. }
  119. else
  120. {
  121. unlink($path.'/'.$filename);
  122. }
  123. }
  124. }
  125. @closedir($current_dir);
  126. if ($del_dir == TRUE AND $level > 0)
  127. {
  128. @rmdir($path);
  129. }
  130. }
  131. }
  132. // ------------------------------------------------------------------------
  133. /**
  134. * Get Filenames
  135. *
  136. * Reads the specified directory and builds an array containing the filenames.
  137. * Any sub-folders contained within the specified path are read as well.
  138. *
  139. * @access public
  140. * @param string path to source
  141. * @param bool whether to include the path as part of the filename
  142. * @return array
  143. */
  144. if (! function_exists('get_filenames'))
  145. {
  146. function get_filenames($source_dir, $include_path = FALSE)
  147. {
  148. $_filedata = array();
  149. if ($fp = @opendir($source_dir))
  150. {
  151. while (FALSE !== ($file = readdir($fp)))
  152. {
  153. if (@is_dir($source_dir.$file) && substr($file, 0, 1) != '.')
  154. {
  155. get_filenames($source_dir.$file."/", $include_path);
  156. }
  157. elseif (substr($file, 0, 1) != ".")
  158. {
  159. $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
  160. }
  161. }
  162. return $_filedata;
  163. }
  164. }
  165. }
  166. // --------------------------------------------------------------------
  167. ?>