File.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * Convenience class for reading, writing and appending to files.
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @since CakePHP(tm) v 0.2.9
  17. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  18. */
  19. namespace Cake\Utility;
  20. /**
  21. * Convenience class for reading, writing and appending to files.
  22. *
  23. */
  24. class File {
  25. /**
  26. * Folder object of the file
  27. *
  28. * @var Folder
  29. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$Folder
  30. */
  31. public $Folder = null;
  32. /**
  33. * File name
  34. *
  35. * @var string
  36. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$name
  37. */
  38. public $name = null;
  39. /**
  40. * File info
  41. *
  42. * @var array
  43. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$info
  44. */
  45. public $info = array();
  46. /**
  47. * Holds the file handler resource if the file is opened
  48. *
  49. * @var resource
  50. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$handle
  51. */
  52. public $handle = null;
  53. /**
  54. * Enable locking for file reading and writing
  55. *
  56. * @var boolean
  57. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$lock
  58. */
  59. public $lock = null;
  60. /**
  61. * Path property
  62. *
  63. * Current file's absolute path
  64. *
  65. * @var mixed null
  66. * http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$path
  67. */
  68. public $path = null;
  69. /**
  70. * Constructor
  71. *
  72. * @param string $path Path to file
  73. * @param boolean $create Create file if it does not exist (if true)
  74. * @param integer $mode Mode to apply to the folder holding the file
  75. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File
  76. */
  77. public function __construct($path, $create = false, $mode = 0755) {
  78. $this->Folder = new Folder(dirname($path), $create, $mode);
  79. if (!is_dir($path)) {
  80. $this->name = basename($path);
  81. }
  82. $this->pwd();
  83. $create && !$this->exists() && $this->safe($path) && $this->create();
  84. }
  85. /**
  86. * Closes the current file if it is opened
  87. *
  88. */
  89. public function __destruct() {
  90. $this->close();
  91. }
  92. /**
  93. * Creates the file.
  94. *
  95. * @return boolean Success
  96. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::create
  97. */
  98. public function create() {
  99. $dir = $this->Folder->pwd();
  100. if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
  101. if (touch($this->path)) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /**
  108. * Opens the current file with a given $mode
  109. *
  110. * @param string $mode A valid 'fopen' mode string (r|w|a ...)
  111. * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
  112. * @return boolean True on success, false on failure
  113. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::open
  114. */
  115. public function open($mode = 'r', $force = false) {
  116. if (!$force && is_resource($this->handle)) {
  117. return true;
  118. }
  119. if ($this->exists() === false) {
  120. if ($this->create() === false) {
  121. return false;
  122. }
  123. }
  124. $this->handle = fopen($this->path, $mode);
  125. if (is_resource($this->handle)) {
  126. return true;
  127. }
  128. return false;
  129. }
  130. /**
  131. * Return the contents of this file as a string.
  132. *
  133. * @param string $bytes where to start
  134. * @param string $mode A `fread` compatible mode.
  135. * @param boolean $force If true then the file will be re-opened even if its already opened, otherwise it won't
  136. * @return mixed string on success, false on failure
  137. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::read
  138. */
  139. public function read($bytes = false, $mode = 'rb', $force = false) {
  140. if ($bytes === false && $this->lock === null) {
  141. return file_get_contents($this->path);
  142. }
  143. if ($this->open($mode, $force) === false) {
  144. return false;
  145. }
  146. if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
  147. return false;
  148. }
  149. if (is_int($bytes)) {
  150. return fread($this->handle, $bytes);
  151. }
  152. $data = '';
  153. while (!feof($this->handle)) {
  154. $data .= fgets($this->handle, 4096);
  155. }
  156. if ($this->lock !== null) {
  157. flock($this->handle, LOCK_UN);
  158. }
  159. if ($bytes === false) {
  160. $this->close();
  161. }
  162. return trim($data);
  163. }
  164. /**
  165. * Sets or gets the offset for the currently opened file.
  166. *
  167. * @param integer|boolean $offset The $offset in bytes to seek. If set to false then the current offset is returned.
  168. * @param integer $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
  169. * @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
  170. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::offset
  171. */
  172. public function offset($offset = false, $seek = SEEK_SET) {
  173. if ($offset === false) {
  174. if (is_resource($this->handle)) {
  175. return ftell($this->handle);
  176. }
  177. } elseif ($this->open() === true) {
  178. return fseek($this->handle, $offset, $seek) === 0;
  179. }
  180. return false;
  181. }
  182. /**
  183. * Prepares a ASCII string for writing. Converts line endings to the
  184. * correct terminator for the current platform. If Windows, "\r\n" will be used,
  185. * all other platforms will use "\n"
  186. *
  187. * @param string $data Data to prepare for writing.
  188. * @param boolean $forceWindows
  189. * @return string The with converted line endings.
  190. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::prepare
  191. */
  192. public static function prepare($data, $forceWindows = false) {
  193. $lineBreak = "\n";
  194. if (DIRECTORY_SEPARATOR === '\\' || $forceWindows === true) {
  195. $lineBreak = "\r\n";
  196. }
  197. return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
  198. }
  199. /**
  200. * Write given data to this file.
  201. *
  202. * @param string $data Data to write to this File.
  203. * @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
  204. * @param string $force Force the file to open
  205. * @return boolean Success
  206. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::write
  207. */
  208. public function write($data, $mode = 'w', $force = false) {
  209. $success = false;
  210. if ($this->open($mode, $force) === true) {
  211. if ($this->lock !== null) {
  212. if (flock($this->handle, LOCK_EX) === false) {
  213. return false;
  214. }
  215. }
  216. if (fwrite($this->handle, $data) !== false) {
  217. $success = true;
  218. }
  219. if ($this->lock !== null) {
  220. flock($this->handle, LOCK_UN);
  221. }
  222. }
  223. return $success;
  224. }
  225. /**
  226. * Append given data string to this file.
  227. *
  228. * @param string $data Data to write
  229. * @param string $force Force the file to open
  230. * @return boolean Success
  231. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::append
  232. */
  233. public function append($data, $force = false) {
  234. return $this->write($data, 'a', $force);
  235. }
  236. /**
  237. * Closes the current file if it is opened.
  238. *
  239. * @return boolean True if closing was successful or file was already closed, otherwise false
  240. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::close
  241. */
  242. public function close() {
  243. if (!is_resource($this->handle)) {
  244. return true;
  245. }
  246. return fclose($this->handle);
  247. }
  248. /**
  249. * Deletes the file.
  250. *
  251. * @return boolean Success
  252. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::delete
  253. */
  254. public function delete() {
  255. if (is_resource($this->handle)) {
  256. fclose($this->handle);
  257. $this->handle = null;
  258. }
  259. if ($this->exists()) {
  260. return unlink($this->path);
  261. }
  262. return false;
  263. }
  264. /**
  265. * Returns the file info as an array with the following keys:
  266. *
  267. * - dirname
  268. * - basename
  269. * - extension
  270. * - filename
  271. * - filesize
  272. * - mime
  273. *
  274. * @return array File information.
  275. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::info
  276. */
  277. public function info() {
  278. if (!$this->info) {
  279. $this->info = pathinfo($this->path);
  280. }
  281. if (!isset($this->info['filename'])) {
  282. $this->info['filename'] = $this->name();
  283. }
  284. if (!isset($this->info['filesize'])) {
  285. $this->info['filesize'] = $this->size();
  286. }
  287. if (!isset($this->info['mime'])) {
  288. $this->info['mime'] = $this->mime();
  289. }
  290. return $this->info;
  291. }
  292. /**
  293. * Returns the file extension.
  294. *
  295. * @return string The file extension
  296. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::ext
  297. */
  298. public function ext() {
  299. if (!$this->info) {
  300. $this->info();
  301. }
  302. if (isset($this->info['extension'])) {
  303. return $this->info['extension'];
  304. }
  305. return false;
  306. }
  307. /**
  308. * Returns the file name without extension.
  309. *
  310. * @return string The file name without extension.
  311. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::name
  312. */
  313. public function name() {
  314. if (!$this->info) {
  315. $this->info();
  316. }
  317. if (isset($this->info['extension'])) {
  318. return basename($this->name, '.' . $this->info['extension']);
  319. } elseif ($this->name) {
  320. return $this->name;
  321. }
  322. return false;
  323. }
  324. /**
  325. * Makes file name safe for saving
  326. *
  327. * @param string $name The name of the file to make safe if different from $this->name
  328. * @param string $ext The name of the extension to make safe if different from $this->ext
  329. * @return string $ext The extension of the file
  330. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::safe
  331. */
  332. public function safe($name = null, $ext = null) {
  333. if (!$name) {
  334. $name = $this->name;
  335. }
  336. if (!$ext) {
  337. $ext = $this->ext();
  338. }
  339. return preg_replace("/(?:[^\w\.-]+)/", "_", basename($name, $ext));
  340. }
  341. /**
  342. * Get md5 Checksum of file with previous check of Filesize
  343. *
  344. * @param integer|boolean $maxsize in MB or true to force
  345. * @return string|false md5 Checksum {@link http://php.net/md5_file See md5_file()}, or false in case of an error
  346. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::md5
  347. */
  348. public function md5($maxsize = 5) {
  349. if ($maxsize === true) {
  350. return md5_file($this->path);
  351. }
  352. $size = $this->size();
  353. if ($size && $size < ($maxsize * 1024) * 1024) {
  354. return md5_file($this->path);
  355. }
  356. return false;
  357. }
  358. /**
  359. * Returns the full path of the file.
  360. *
  361. * @return string Full path to the file
  362. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::pwd
  363. */
  364. public function pwd() {
  365. if ($this->path === null) {
  366. $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
  367. }
  368. return $this->path;
  369. }
  370. /**
  371. * Returns true if the file exists.
  372. *
  373. * @return boolean True if it exists, false otherwise
  374. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::exists
  375. */
  376. public function exists() {
  377. $this->clearStatCache();
  378. return (file_exists($this->path) && is_file($this->path));
  379. }
  380. /**
  381. * Returns the "chmod" (permissions) of the file.
  382. *
  383. * @return string|false Permissions for the file, or false in case of an error
  384. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::perms
  385. */
  386. public function perms() {
  387. if ($this->exists()) {
  388. return substr(sprintf('%o', fileperms($this->path)), -4);
  389. }
  390. return false;
  391. }
  392. /**
  393. * Returns the file size
  394. *
  395. * @return integer|false Size of the file in bytes, or false in case of an error
  396. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::size
  397. */
  398. public function size() {
  399. if ($this->exists()) {
  400. return filesize($this->path);
  401. }
  402. return false;
  403. }
  404. /**
  405. * Returns true if the file is writable.
  406. *
  407. * @return boolean True if it's writable, false otherwise
  408. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::writable
  409. */
  410. public function writable() {
  411. return is_writable($this->path);
  412. }
  413. /**
  414. * Returns true if the File is executable.
  415. *
  416. * @return boolean True if it's executable, false otherwise
  417. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::executable
  418. */
  419. public function executable() {
  420. return is_executable($this->path);
  421. }
  422. /**
  423. * Returns true if the file is readable.
  424. *
  425. * @return boolean True if file is readable, false otherwise
  426. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::readable
  427. */
  428. public function readable() {
  429. return is_readable($this->path);
  430. }
  431. /**
  432. * Returns the file's owner.
  433. *
  434. * @return integer|false The file owner, or false in case of an error
  435. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::owner
  436. */
  437. public function owner() {
  438. if ($this->exists()) {
  439. return fileowner($this->path);
  440. }
  441. return false;
  442. }
  443. /**
  444. * Returns the file's group.
  445. *
  446. * @return integer|false The file group, or false in case of an error
  447. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::group
  448. */
  449. public function group() {
  450. if ($this->exists()) {
  451. return filegroup($this->path);
  452. }
  453. return false;
  454. }
  455. /**
  456. * Returns last access time.
  457. *
  458. * @return integer|false Timestamp of last access time, or false in case of an error
  459. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastAccess
  460. */
  461. public function lastAccess() {
  462. if ($this->exists()) {
  463. return fileatime($this->path);
  464. }
  465. return false;
  466. }
  467. /**
  468. * Returns last modified time.
  469. *
  470. * @return integer|false Timestamp of last modification, or false in case of an error
  471. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastChange
  472. */
  473. public function lastChange() {
  474. if ($this->exists()) {
  475. return filemtime($this->path);
  476. }
  477. return false;
  478. }
  479. /**
  480. * Returns the current folder.
  481. *
  482. * @return Folder Current folder
  483. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::Folder
  484. */
  485. public function folder() {
  486. return $this->Folder;
  487. }
  488. /**
  489. * Copy the File to $dest
  490. *
  491. * @param string $dest Destination for the copy
  492. * @param boolean $overwrite Overwrite $dest if exists
  493. * @return boolean Success
  494. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::copy
  495. */
  496. public function copy($dest, $overwrite = true) {
  497. if (!$this->exists() || is_file($dest) && !$overwrite) {
  498. return false;
  499. }
  500. return copy($this->path, $dest);
  501. }
  502. /**
  503. * Get the mime type of the file. Uses the finfo extension if
  504. * its available, otherwise falls back to mime_content_type
  505. *
  506. * @return false|string The mimetype of the file, or false if reading fails.
  507. */
  508. public function mime() {
  509. if (!$this->exists()) {
  510. return false;
  511. }
  512. if (function_exists('finfo_open')) {
  513. $finfo = finfo_open(FILEINFO_MIME);
  514. $finfo = finfo_file($finfo, $this->pwd());
  515. if (!$finfo) {
  516. return false;
  517. }
  518. list($type, $charset) = explode(';', $finfo);
  519. return $type;
  520. }
  521. if (function_exists('mime_content_type')) {
  522. return mime_content_type($this->pwd());
  523. }
  524. return false;
  525. }
  526. /**
  527. * Clear PHP's internal stat cache
  528. *
  529. * @param boolean $all Clear all cache or not. Passing false will clear
  530. * the stat cache for the current path only.
  531. * @return void
  532. */
  533. public function clearStatCache($all = false) {
  534. if ($all === false) {
  535. return clearstatcache(true, $this->path);
  536. }
  537. return clearstatcache();
  538. }
  539. }