PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/system/helpers/file_helper.php

https://gitlab.com/zanderwong/admin
PHP | 402 lines | 202 code | 51 blank | 149 comment | 57 complexity | 0b718179d82b4b04f4043947c69a2e56 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 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, 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. function read_file($file) {
  37. if (!file_exists($file)) {
  38. return FALSE;
  39. }
  40. if (function_exists('file_get_contents')) {
  41. return file_get_contents($file);
  42. }
  43. if (!$fp = @fopen($file, FOPEN_READ)) {
  44. return FALSE;
  45. }
  46. flock($fp, LOCK_SH);
  47. $data = '';
  48. if (filesize($file) > 0) {
  49. $data =& fread($fp, filesize($file));
  50. }
  51. flock($fp, LOCK_UN);
  52. fclose($fp);
  53. return $data;
  54. }
  55. }
  56. // ------------------------------------------------------------------------
  57. /**
  58. * Write File
  59. *
  60. * Writes data to the file specified in the path.
  61. * Creates a new file if non-existent.
  62. *
  63. * @access public
  64. * @param string path to file
  65. * @param string file data
  66. * @return bool
  67. */
  68. if (!function_exists('write_file')) {
  69. function write_file($path, $data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) {
  70. if (!$fp = @fopen($path, $mode)) {
  71. return FALSE;
  72. }
  73. flock($fp, LOCK_EX);
  74. fwrite($fp, $data);
  75. flock($fp, LOCK_UN);
  76. fclose($fp);
  77. return TRUE;
  78. }
  79. }
  80. // ------------------------------------------------------------------------
  81. /**
  82. * Delete Files
  83. *
  84. * Deletes all files contained in the supplied directory path.
  85. * Files must be writable or owned by the system in order to be deleted.
  86. * If the second parameter is set to TRUE, any directories contained
  87. * within the supplied base directory will be nuked as well.
  88. *
  89. * @access public
  90. * @param string path to file
  91. * @param bool whether to delete any directories found in the path
  92. * @return bool
  93. */
  94. if (!function_exists('delete_files')) {
  95. function delete_files($path, $del_dir = FALSE, $level = 0) {
  96. // Trim the trailing slash
  97. $path = rtrim($path, DIRECTORY_SEPARATOR);
  98. if (!$current_dir = @opendir($path)) {
  99. return FALSE;
  100. }
  101. while (FALSE !== ($filename = @readdir($current_dir))) {
  102. if ($filename != "." and $filename != "..") {
  103. if (is_dir($path . DIRECTORY_SEPARATOR . $filename)) {
  104. // Ignore empty folders
  105. if (substr($filename, 0, 1) != '.') {
  106. delete_files($path . DIRECTORY_SEPARATOR . $filename, $del_dir, $level + 1);
  107. }
  108. } else {
  109. unlink($path . DIRECTORY_SEPARATOR . $filename);
  110. }
  111. }
  112. }
  113. @closedir($current_dir);
  114. if ($del_dir == TRUE AND $level > 0) {
  115. return @rmdir($path);
  116. }
  117. return TRUE;
  118. }
  119. }
  120. // ------------------------------------------------------------------------
  121. /**
  122. * Get Filenames
  123. *
  124. * Reads the specified directory and builds an array containing the filenames.
  125. * Any sub-folders contained within the specified path are read as well.
  126. *
  127. * @access public
  128. * @param string path to source
  129. * @param bool whether to include the path as part of the filename
  130. * @param bool internal variable to determine recursion status - do not use in calls
  131. * @return array
  132. */
  133. if (!function_exists('get_filenames')) {
  134. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE) {
  135. static $_filedata = array();
  136. if ($fp = @opendir($source_dir)) {
  137. // reset the array and make sure $source_dir has a trailing slash on the initial call
  138. if ($_recursion === FALSE) {
  139. $_filedata = array();
  140. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  141. }
  142. while (FALSE !== ($file = readdir($fp))) {
  143. if (@is_dir($source_dir . $file) && strncmp($file, '.', 1) !== 0) {
  144. get_filenames($source_dir . $file . DIRECTORY_SEPARATOR, $include_path, TRUE);
  145. } elseif (strncmp($file, '.', 1) !== 0) {
  146. $_filedata[] = ($include_path == TRUE) ? $source_dir . $file : $file;
  147. }
  148. }
  149. return $_filedata;
  150. } else {
  151. return FALSE;
  152. }
  153. }
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Get Directory File Information
  158. *
  159. * Reads the specified directory and builds an array containing the filenames,
  160. * filesize, dates, and permissions
  161. *
  162. * Any sub-folders contained within the specified path are read as well.
  163. *
  164. * @access public
  165. * @param string path to source
  166. * @param bool Look only at the top level directory specified?
  167. * @param bool internal variable to determine recursion status - do not use in calls
  168. * @return array
  169. */
  170. if (!function_exists('get_dir_file_info')) {
  171. function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE) {
  172. static $_filedata = array();
  173. $relative_path = $source_dir;
  174. if ($fp = @opendir($source_dir)) {
  175. // reset the array and make sure $source_dir has a trailing slash on the initial call
  176. if ($_recursion === FALSE) {
  177. $_filedata = array();
  178. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  179. }
  180. // foreach (scandir($source_dir, 1) as $file) // In addition to being PHP5+, scandir() is simply not as fast
  181. while (FALSE !== ($file = readdir($fp))) {
  182. if (@is_dir($source_dir . $file) AND strncmp($file, '.', 1) !== 0 AND $top_level_only === FALSE) {
  183. get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, TRUE);
  184. } elseif (strncmp($file, '.', 1) !== 0) {
  185. $_filedata[$file] = get_file_info($source_dir . $file);
  186. $_filedata[$file]['relative_path'] = $relative_path;
  187. }
  188. }
  189. return $_filedata;
  190. } else {
  191. return FALSE;
  192. }
  193. }
  194. }
  195. // --------------------------------------------------------------------
  196. /**
  197. * Get File Info
  198. *
  199. * Given a file and path, returns the name, path, size, date modified
  200. * Second parameter allows you to explicitly declare what information you want returned
  201. * Options are: name, server_path, size, date, readable, writable, executable, fileperms
  202. * Returns FALSE if the file cannot be found.
  203. *
  204. * @access public
  205. * @param string path to file
  206. * @param mixed array or comma separated string of information returned
  207. * @return array
  208. */
  209. if (!function_exists('get_file_info')) {
  210. function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date')) {
  211. if (!file_exists($file)) {
  212. return FALSE;
  213. }
  214. if (is_string($returned_values)) {
  215. $returned_values = explode(',', $returned_values);
  216. }
  217. foreach ($returned_values as $key) {
  218. switch ($key) {
  219. case 'name':
  220. $fileinfo['name'] = substr(strrchr($file, DIRECTORY_SEPARATOR), 1);
  221. break;
  222. case 'server_path':
  223. $fileinfo['server_path'] = $file;
  224. break;
  225. case 'size':
  226. $fileinfo['size'] = filesize($file);
  227. break;
  228. case 'date':
  229. $fileinfo['date'] = filemtime($file);
  230. break;
  231. case 'readable':
  232. $fileinfo['readable'] = is_readable($file);
  233. break;
  234. case 'writable':
  235. // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
  236. $fileinfo['writable'] = is_writable($file);
  237. break;
  238. case 'executable':
  239. $fileinfo['executable'] = is_executable($file);
  240. break;
  241. case 'fileperms':
  242. $fileinfo['fileperms'] = fileperms($file);
  243. break;
  244. }
  245. }
  246. return $fileinfo;
  247. }
  248. }
  249. // --------------------------------------------------------------------
  250. /**
  251. * Get Mime by Extension
  252. *
  253. * Translates a file extension into a mime type based on config/mimes.php.
  254. * Returns FALSE if it can't determine the type, or open the mime config file
  255. *
  256. * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
  257. * It should NOT be trusted, and should certainly NOT be used for security
  258. *
  259. * @access public
  260. * @param string path to file
  261. * @return mixed
  262. */
  263. if (!function_exists('get_mime_by_extension')) {
  264. function get_mime_by_extension($file) {
  265. $extension = strtolower(substr(strrchr($file, '.'), 1));
  266. global $mimes;
  267. if (!is_array($mimes)) {
  268. if (defined('ENVIRONMENT') AND is_file(APPPATH . 'config/' . ENVIRONMENT . '/mimes.php')) {
  269. include(APPPATH . 'config/' . ENVIRONMENT . '/mimes.php');
  270. } elseif (is_file(APPPATH . 'config/mimes.php')) {
  271. include(APPPATH . 'config/mimes.php');
  272. }
  273. if (!is_array($mimes)) {
  274. return FALSE;
  275. }
  276. }
  277. if (array_key_exists($extension, $mimes)) {
  278. if (is_array($mimes[$extension])) {
  279. // Multiple mime types, just give the first one
  280. return current($mimes[$extension]);
  281. } else {
  282. return $mimes[$extension];
  283. }
  284. } else {
  285. return FALSE;
  286. }
  287. }
  288. }
  289. // --------------------------------------------------------------------
  290. /**
  291. * Symbolic Permissions
  292. *
  293. * Takes a numeric value representing a file's permissions and returns
  294. * standard symbolic notation representing that value
  295. *
  296. * @access public
  297. * @param int
  298. * @return string
  299. */
  300. if (!function_exists('symbolic_permissions')) {
  301. function symbolic_permissions($perms) {
  302. if (($perms & 0xC000) == 0xC000) {
  303. $symbolic = 's'; // Socket
  304. } elseif (($perms & 0xA000) == 0xA000) {
  305. $symbolic = 'l'; // Symbolic Link
  306. } elseif (($perms & 0x8000) == 0x8000) {
  307. $symbolic = '-'; // Regular
  308. } elseif (($perms & 0x6000) == 0x6000) {
  309. $symbolic = 'b'; // Block special
  310. } elseif (($perms & 0x4000) == 0x4000) {
  311. $symbolic = 'd'; // Directory
  312. } elseif (($perms & 0x2000) == 0x2000) {
  313. $symbolic = 'c'; // Character special
  314. } elseif (($perms & 0x1000) == 0x1000) {
  315. $symbolic = 'p'; // FIFO pipe
  316. } else {
  317. $symbolic = 'u'; // Unknown
  318. }
  319. // Owner
  320. $symbolic .= (($perms & 0x0100) ? 'r' : '-');
  321. $symbolic .= (($perms & 0x0080) ? 'w' : '-');
  322. $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x') : (($perms & 0x0800) ? 'S' : '-'));
  323. // Group
  324. $symbolic .= (($perms & 0x0020) ? 'r' : '-');
  325. $symbolic .= (($perms & 0x0010) ? 'w' : '-');
  326. $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x') : (($perms & 0x0400) ? 'S' : '-'));
  327. // World
  328. $symbolic .= (($perms & 0x0004) ? 'r' : '-');
  329. $symbolic .= (($perms & 0x0002) ? 'w' : '-');
  330. $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x') : (($perms & 0x0200) ? 'T' : '-'));
  331. return $symbolic;
  332. }
  333. }
  334. // --------------------------------------------------------------------
  335. /**
  336. * Octal Permissions
  337. *
  338. * Takes a numeric value representing a file's permissions and returns
  339. * a three character string representing the file's octal permissions
  340. *
  341. * @access public
  342. * @param int
  343. * @return string
  344. */
  345. if (!function_exists('octal_permissions')) {
  346. function octal_permissions($perms) {
  347. return substr(sprintf('%o', $perms), -3);
  348. }
  349. }
  350. /* End of file file_helper.php */
  351. /* Location: ./system/helpers/file_helper.php */