File.php 15 KB

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