PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/system/helpers/file_helper.php

https://github.com/katzgrau/notes
PHP | 465 lines | 242 code | 44 blank | 179 comment | 41 complexity | aaa262c839fbc05c3682cfbe336fad5f 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) 2008, 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_helper.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, FOPEN_READ))
  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 = FOPEN_WRITE_CREATE_DESTRUCTIVE)
  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. // Ignore empty folders
  118. if (substr($filename, 0, 1) != '.')
  119. {
  120. delete_files($path.'/'.$filename, $del_dir, $level + 1);
  121. }
  122. }
  123. else
  124. {
  125. unlink($path.'/'.$filename);
  126. }
  127. }
  128. }
  129. @closedir($current_dir);
  130. if ($del_dir == TRUE AND $level > 0)
  131. {
  132. @rmdir($path);
  133. }
  134. }
  135. }
  136. // ------------------------------------------------------------------------
  137. /**
  138. * Get Filenames
  139. *
  140. * Reads the specified directory and builds an array containing the filenames.
  141. * Any sub-folders contained within the specified path are read as well.
  142. *
  143. * @access public
  144. * @param string path to source
  145. * @param bool whether to include the path as part of the filename
  146. * @param bool internal variable to determine recursion status - do not use in calls
  147. * @return array
  148. */
  149. if ( ! function_exists('get_filenames'))
  150. {
  151. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
  152. {
  153. static $_filedata = array();
  154. if ($fp = @opendir($source_dir))
  155. {
  156. // reset the array and make sure $source_dir has a trailing slash on the initial call
  157. if ($_recursion === FALSE)
  158. {
  159. $_filedata = array();
  160. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  161. }
  162. while (FALSE !== ($file = readdir($fp)))
  163. {
  164. if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
  165. {
  166. get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
  167. }
  168. elseif (strncmp($file, '.', 1) !== 0)
  169. {
  170. $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
  171. }
  172. }
  173. return $_filedata;
  174. }
  175. else
  176. {
  177. return FALSE;
  178. }
  179. }
  180. }
  181. // --------------------------------------------------------------------
  182. /**
  183. * Get Directory File Information
  184. *
  185. * Reads the specified directory and builds an array containing the filenames,
  186. * filesize, dates, and permissions
  187. *
  188. * Any sub-folders contained within the specified path are read as well.
  189. *
  190. * @access public
  191. * @param string path to source
  192. * @param bool whether to include the path as part of the filename
  193. * @param bool internal variable to determine recursion status - do not use in calls
  194. * @return array
  195. */
  196. if ( ! function_exists('get_dir_file_info'))
  197. {
  198. function get_dir_file_info($source_dir, $include_path = FALSE, $_recursion = FALSE)
  199. {
  200. static $_filedata = array();
  201. $relative_path = $source_dir;
  202. if ($fp = @opendir($source_dir))
  203. {
  204. // reset the array and make sure $source_dir has a trailing slash on the initial call
  205. if ($_recursion === FALSE)
  206. {
  207. $_filedata = array();
  208. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  209. }
  210. while (FALSE !== ($file = readdir($fp)))
  211. {
  212. if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
  213. {
  214. get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
  215. }
  216. elseif (strncmp($file, '.', 1) !== 0)
  217. {
  218. $_filedata[$file] = get_file_info($source_dir.$file);
  219. $_filedata[$file]['relative_path'] = $relative_path;
  220. }
  221. }
  222. return $_filedata;
  223. }
  224. else
  225. {
  226. return FALSE;
  227. }
  228. }
  229. }
  230. // --------------------------------------------------------------------
  231. /**
  232. * Get File Info
  233. *
  234. * Given a file and path, returns the name, path, size, date modified
  235. * Second parameter allows you to explicitly declare what information you want returned
  236. * Options are: name, server_path, size, date, readable, writable, executable, fileperms
  237. * Returns FALSE if the file cannot be found.
  238. *
  239. * @access public
  240. * @param string path to file
  241. * @param mixed array or comma separated string of information returned
  242. * @return array
  243. */
  244. if ( ! function_exists('get_file_info'))
  245. {
  246. function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
  247. {
  248. if ( ! file_exists($file))
  249. {
  250. return FALSE;
  251. }
  252. if (is_string($returned_values))
  253. {
  254. $returned_values = explode(',', $returned_values);
  255. }
  256. foreach ($returned_values as $key)
  257. {
  258. switch ($key)
  259. {
  260. case 'name':
  261. $fileinfo['name'] = substr(strrchr($file, '/'), 1);
  262. break;
  263. case 'server_path':
  264. $fileinfo['server_path'] = $file;
  265. break;
  266. case 'size':
  267. $fileinfo['size'] = filesize($file);
  268. break;
  269. case 'date':
  270. $fileinfo['date'] = filectime($file);
  271. break;
  272. case 'readable':
  273. $fileinfo['readable'] = is_readable($file);
  274. break;
  275. case 'writable':
  276. // There are known problems using is_weritable on IIS. It may not be reliable - consider fileperms()
  277. $fileinfo['writable'] = is_writable($file);
  278. break;
  279. case 'executable':
  280. $fileinfo['executable'] = is_executable($file);
  281. break;
  282. case 'fileperms':
  283. $fileinfo['fileperms'] = fileperms($file);
  284. break;
  285. }
  286. }
  287. return $fileinfo;
  288. }
  289. }
  290. // --------------------------------------------------------------------
  291. /**
  292. * Get Mime by Extension
  293. *
  294. * Translates a file extension into a mime type based on config/mimes.php.
  295. * Returns FALSE if it can't determine the type, or open the mime config file
  296. *
  297. * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
  298. * It should NOT be trusted, and should certainly NOT be used for security
  299. *
  300. * @access public
  301. * @param string path to file
  302. * @return mixed
  303. */
  304. if ( ! function_exists('get_mime_by_extension'))
  305. {
  306. function get_mime_by_extension($file)
  307. {
  308. $extension = substr(strrchr($file, '.'), 1);
  309. global $mimes;
  310. if ( ! is_array($mimes))
  311. {
  312. if ( ! require_once(APPPATH.'config/mimes.php'))
  313. {
  314. return FALSE;
  315. }
  316. }
  317. if (array_key_exists($extension, $mimes))
  318. {
  319. if (is_array($mimes[$extension]))
  320. {
  321. // Multiple mime types, just give the first one
  322. return current($mimes[$extension]);
  323. }
  324. else
  325. {
  326. return $mimes[$extension];
  327. }
  328. }
  329. else
  330. {
  331. return FALSE;
  332. }
  333. }
  334. }
  335. // --------------------------------------------------------------------
  336. /**
  337. * Symbolic Permissions
  338. *
  339. * Takes a numeric value representing a file's permissions and returns
  340. * standard symbolic notation representing that value
  341. *
  342. * @access public
  343. * @param int
  344. * @return string
  345. */
  346. if ( ! function_exists('symbolic_permissions'))
  347. {
  348. function symbolic_permissions($perms)
  349. {
  350. if (($perms & 0xC000) == 0xC000)
  351. {
  352. $symbolic = 's'; // Socket
  353. }
  354. elseif (($perms & 0xA000) == 0xA000)
  355. {
  356. $symbolic = 'l'; // Symbolic Link
  357. }
  358. elseif (($perms & 0x8000) == 0x8000)
  359. {
  360. $symbolic = '-'; // Regular
  361. }
  362. elseif (($perms & 0x6000) == 0x6000)
  363. {
  364. $symbolic = 'b'; // Block special
  365. }
  366. elseif (($perms & 0x4000) == 0x4000)
  367. {
  368. $symbolic = 'd'; // Directory
  369. }
  370. elseif (($perms & 0x2000) == 0x2000)
  371. {
  372. $symbolic = 'c'; // Character special
  373. }
  374. elseif (($perms & 0x1000) == 0x1000)
  375. {
  376. $symbolic = 'p'; // FIFO pipe
  377. }
  378. else
  379. {
  380. $symbolic = 'u'; // Unknown
  381. }
  382. // Owner
  383. $symbolic .= (($perms & 0x0100) ? 'r' : '-');
  384. $symbolic .= (($perms & 0x0080) ? 'w' : '-');
  385. $symbolic .= (($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
  386. // Group
  387. $symbolic .= (($perms & 0x0020) ? 'r' : '-');
  388. $symbolic .= (($perms & 0x0010) ? 'w' : '-');
  389. $symbolic .= (($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
  390. // World
  391. $symbolic .= (($perms & 0x0004) ? 'r' : '-');
  392. $symbolic .= (($perms & 0x0002) ? 'w' : '-');
  393. $symbolic .= (($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
  394. return $symbolic;
  395. }
  396. }
  397. // --------------------------------------------------------------------
  398. /**
  399. * Octal Permissions
  400. *
  401. * Takes a numeric value representing a file's permissions and returns
  402. * a three character string representing the file's octal permissions
  403. *
  404. * @access public
  405. * @param int
  406. * @return string
  407. */
  408. if ( ! function_exists('octal_permissions'))
  409. {
  410. function octal_permissions($perms)
  411. {
  412. return substr(sprintf('%o', $perms), -3);
  413. }
  414. }
  415. /* End of file file_helper.php */
  416. /* Location: ./system/helpers/file_helper.php */