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