PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/file_helper.php

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