File.php 15 KB

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