file_helper.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter File Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/file_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('read_file'))
  50. {
  51. /**
  52. * Read File
  53. *
  54. * Opens the file specified in the path and returns it as a string.
  55. *
  56. * @todo Remove in version 3.1+.
  57. * @deprecated 3.0.0 It is now just an alias for PHP's native file_get_contents().
  58. * @param string $file Path to file
  59. * @return string File contents
  60. */
  61. function read_file($file)
  62. {
  63. return @file_get_contents($file);
  64. }
  65. }
  66. // ------------------------------------------------------------------------
  67. if ( ! function_exists('write_file'))
  68. {
  69. /**
  70. * Write File
  71. *
  72. * Writes data to the file specified in the path.
  73. * Creates a new file if non-existent.
  74. *
  75. * @param string $path File path
  76. * @param string $data Data to write
  77. * @param string $mode fopen() mode (default: 'wb')
  78. * @return bool
  79. */
  80. function write_file($path, $data, $mode = 'wb')
  81. {
  82. if ( ! $fp = @fopen($path, $mode))
  83. {
  84. return FALSE;
  85. }
  86. flock($fp, LOCK_EX);
  87. for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
  88. {
  89. if (($result = fwrite($fp, substr($data, $written))) === FALSE)
  90. {
  91. break;
  92. }
  93. }
  94. flock($fp, LOCK_UN);
  95. fclose($fp);
  96. return is_int($result);
  97. }
  98. }
  99. // ------------------------------------------------------------------------
  100. if ( ! function_exists('delete_files'))
  101. {
  102. /**
  103. * Delete Files
  104. *
  105. * Deletes all files contained in the supplied directory path.
  106. * Files must be writable or owned by the system in order to be deleted.
  107. * If the second parameter is set to TRUE, any directories contained
  108. * within the supplied base directory will be nuked as well.
  109. *
  110. * @param string $path File path
  111. * @param bool $del_dir Whether to delete any directories found in the path
  112. * @param bool $htdocs Whether to skip deleting .htaccess and index page files
  113. * @param int $_level Current directory depth level (default: 0; internal use only)
  114. * @return bool
  115. */
  116. function delete_files($path, $del_dir = FALSE, $htdocs = FALSE, $_level = 0)
  117. {
  118. // Trim the trailing slash
  119. $path = rtrim($path, '/\\');
  120. if ( ! $current_dir = @opendir($path))
  121. {
  122. return FALSE;
  123. }
  124. while (FALSE !== ($filename = @readdir($current_dir)))
  125. {
  126. if ($filename !== '.' && $filename !== '..')
  127. {
  128. $filepath = $path.DIRECTORY_SEPARATOR.$filename;
  129. if (is_dir($filepath) && $filename[0] !== '.' && ! is_link($filepath))
  130. {
  131. delete_files($filepath, $del_dir, $htdocs, $_level + 1);
  132. }
  133. elseif ($htdocs !== TRUE OR ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
  134. {
  135. @unlink($filepath);
  136. }
  137. }
  138. }
  139. closedir($current_dir);
  140. return ($del_dir === TRUE && $_level > 0)
  141. ? @rmdir($path)
  142. : TRUE;
  143. }
  144. }
  145. // ------------------------------------------------------------------------
  146. if ( ! function_exists('get_filenames'))
  147. {
  148. /**
  149. * Get Filenames
  150. *
  151. * Reads the specified directory and builds an array containing the filenames.
  152. * Any sub-folders contained within the specified path are read as well.
  153. *
  154. * @param string path to source
  155. * @param bool whether to include the path as part of the filename
  156. * @param bool internal variable to determine recursion status - do not use in calls
  157. * @return array
  158. */
  159. function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
  160. {
  161. static $_filedata = array();
  162. if ($fp = @opendir($source_dir))
  163. {
  164. // reset the array and make sure $source_dir has a trailing slash on the initial call
  165. if ($_recursion === FALSE)
  166. {
  167. $_filedata = array();
  168. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  169. }
  170. while (FALSE !== ($file = readdir($fp)))
  171. {
  172. if (is_dir($source_dir.$file) && $file[0] !== '.')
  173. {
  174. get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
  175. }
  176. elseif ($file[0] !== '.')
  177. {
  178. $_filedata[] = ($include_path === TRUE) ? $source_dir.$file : $file;
  179. }
  180. }
  181. closedir($fp);
  182. return $_filedata;
  183. }
  184. return FALSE;
  185. }
  186. }
  187. // --------------------------------------------------------------------
  188. if ( ! function_exists('get_dir_file_info'))
  189. {
  190. /**
  191. * Get Directory File Information
  192. *
  193. * Reads the specified directory and builds an array containing the filenames,
  194. * filesize, dates, and permissions
  195. *
  196. * Any sub-folders contained within the specified path are read as well.
  197. *
  198. * @param string path to source
  199. * @param bool Look only at the top level directory specified?
  200. * @param bool internal variable to determine recursion status - do not use in calls
  201. * @return array
  202. */
  203. function get_dir_file_info($source_dir, $top_level_only = TRUE, $_recursion = FALSE)
  204. {
  205. static $_filedata = array();
  206. $relative_path = $source_dir;
  207. if ($fp = @opendir($source_dir))
  208. {
  209. // reset the array and make sure $source_dir has a trailing slash on the initial call
  210. if ($_recursion === FALSE)
  211. {
  212. $_filedata = array();
  213. $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
  214. }
  215. // Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
  216. while (FALSE !== ($file = readdir($fp)))
  217. {
  218. if (is_dir($source_dir.$file) && $file[0] !== '.' && $top_level_only === FALSE)
  219. {
  220. get_dir_file_info($source_dir.$file.DIRECTORY_SEPARATOR, $top_level_only, TRUE);
  221. }
  222. elseif ($file[0] !== '.')
  223. {
  224. $_filedata[$file] = get_file_info($source_dir.$file);
  225. $_filedata[$file]['relative_path'] = $relative_path;
  226. }
  227. }
  228. closedir($fp);
  229. return $_filedata;
  230. }
  231. return FALSE;
  232. }
  233. }
  234. // --------------------------------------------------------------------
  235. if ( ! function_exists('get_file_info'))
  236. {
  237. /**
  238. * Get File Info
  239. *
  240. * Given a file and path, returns the name, path, size, date modified
  241. * Second parameter allows you to explicitly declare what information you want returned
  242. * Options are: name, server_path, size, date, readable, writable, executable, fileperms
  243. * Returns FALSE if the file cannot be found.
  244. *
  245. * @param string path to file
  246. * @param mixed array or comma separated string of information returned
  247. * @return array
  248. */
  249. function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
  250. {
  251. if ( ! file_exists($file))
  252. {
  253. return FALSE;
  254. }
  255. if (is_string($returned_values))
  256. {
  257. $returned_values = explode(',', $returned_values);
  258. }
  259. foreach ($returned_values as $key)
  260. {
  261. switch ($key)
  262. {
  263. case 'name':
  264. $fileinfo['name'] = basename($file);
  265. break;
  266. case 'server_path':
  267. $fileinfo['server_path'] = $file;
  268. break;
  269. case 'size':
  270. $fileinfo['size'] = filesize($file);
  271. break;
  272. case 'date':
  273. $fileinfo['date'] = filemtime($file);
  274. break;
  275. case 'readable':
  276. $fileinfo['readable'] = is_readable($file);
  277. break;
  278. case 'writable':
  279. $fileinfo['writable'] = is_really_writable($file);
  280. break;
  281. case 'executable':
  282. $fileinfo['executable'] = is_executable($file);
  283. break;
  284. case 'fileperms':
  285. $fileinfo['fileperms'] = fileperms($file);
  286. break;
  287. }
  288. }
  289. return $fileinfo;
  290. }
  291. }
  292. // --------------------------------------------------------------------
  293. if ( ! function_exists('get_mime_by_extension'))
  294. {
  295. /**
  296. * Get Mime by Extension
  297. *
  298. * Translates a file extension into a mime type based on config/mimes.php.
  299. * Returns FALSE if it can't determine the type, or open the mime config file
  300. *
  301. * Note: this is NOT an accurate way of determining file mime types, and is here strictly as a convenience
  302. * It should NOT be trusted, and should certainly NOT be used for security
  303. *
  304. * @param string $filename File name
  305. * @return string
  306. */
  307. function get_mime_by_extension($filename)
  308. {
  309. static $mimes;
  310. if ( ! is_array($mimes))
  311. {
  312. $mimes = get_mimes();
  313. if (empty($mimes))
  314. {
  315. return FALSE;
  316. }
  317. }
  318. $extension = strtolower(substr(strrchr($filename, '.'), 1));
  319. if (isset($mimes[$extension]))
  320. {
  321. return is_array($mimes[$extension])
  322. ? current($mimes[$extension]) // Multiple mime types, just give the first one
  323. : $mimes[$extension];
  324. }
  325. return FALSE;
  326. }
  327. }
  328. // --------------------------------------------------------------------
  329. if ( ! function_exists('symbolic_permissions'))
  330. {
  331. /**
  332. * Symbolic Permissions
  333. *
  334. * Takes a numeric value representing a file's permissions and returns
  335. * standard symbolic notation representing that value
  336. *
  337. * @param int $perms Permissions
  338. * @return string
  339. */
  340. function symbolic_permissions($perms)
  341. {
  342. if (($perms & 0xC000) === 0xC000)
  343. {
  344. $symbolic = 's'; // Socket
  345. }
  346. elseif (($perms & 0xA000) === 0xA000)
  347. {
  348. $symbolic = 'l'; // Symbolic Link
  349. }
  350. elseif (($perms & 0x8000) === 0x8000)
  351. {
  352. $symbolic = '-'; // Regular
  353. }
  354. elseif (($perms & 0x6000) === 0x6000)
  355. {
  356. $symbolic = 'b'; // Block special
  357. }
  358. elseif (($perms & 0x4000) === 0x4000)
  359. {
  360. $symbolic = 'd'; // Directory
  361. }
  362. elseif (($perms & 0x2000) === 0x2000)
  363. {
  364. $symbolic = 'c'; // Character special
  365. }
  366. elseif (($perms & 0x1000) === 0x1000)
  367. {
  368. $symbolic = 'p'; // FIFO pipe
  369. }
  370. else
  371. {
  372. $symbolic = 'u'; // Unknown
  373. }
  374. // Owner
  375. $symbolic .= (($perms & 0x0100) ? 'r' : '-')
  376. .(($perms & 0x0080) ? 'w' : '-')
  377. .(($perms & 0x0040) ? (($perms & 0x0800) ? 's' : 'x' ) : (($perms & 0x0800) ? 'S' : '-'));
  378. // Group
  379. $symbolic .= (($perms & 0x0020) ? 'r' : '-')
  380. .(($perms & 0x0010) ? 'w' : '-')
  381. .(($perms & 0x0008) ? (($perms & 0x0400) ? 's' : 'x' ) : (($perms & 0x0400) ? 'S' : '-'));
  382. // World
  383. $symbolic .= (($perms & 0x0004) ? 'r' : '-')
  384. .(($perms & 0x0002) ? 'w' : '-')
  385. .(($perms & 0x0001) ? (($perms & 0x0200) ? 't' : 'x' ) : (($perms & 0x0200) ? 'T' : '-'));
  386. return $symbolic;
  387. }
  388. }
  389. // --------------------------------------------------------------------
  390. if ( ! function_exists('octal_permissions'))
  391. {
  392. /**
  393. * Octal Permissions
  394. *
  395. * Takes a numeric value representing a file's permissions and returns
  396. * a three character string representing the file's octal permissions
  397. *
  398. * @param int $perms Permissions
  399. * @return string
  400. */
  401. function octal_permissions($perms)
  402. {
  403. return substr(sprintf('%o', $perms), -3);
  404. }
  405. }