2008-05-30 11:40:08 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Convenience class for reading, writing and appending to files.
|
|
|
|
*
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2010-12-08 01:12:50 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Convenience class for reading, writing and appending to files.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Utility
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-18 05:12:00 +00:00
|
|
|
class File {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Folder object of the file
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2009-03-17 21:10:28 +00:00
|
|
|
* @var Folder
|
2011-11-18 19:05:30 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$Folder
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $Folder = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* File name
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @var string
|
2011-11-18 19:05:30 +00:00
|
|
|
* http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$name
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $name = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-10 22:22:23 +00:00
|
|
|
* File info
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-11-18 19:05:30 +00:00
|
|
|
* @var array
|
|
|
|
* http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$info
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $info = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Holds the file handler resource if the file is opened
|
|
|
|
*
|
|
|
|
* @var resource
|
2011-11-18 19:05:30 +00:00
|
|
|
* http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$handle
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $handle = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2011-10-10 22:22:23 +00:00
|
|
|
* Enable locking for file reading and writing
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2011-11-18 19:05:30 +00:00
|
|
|
* http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$lock
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $lock = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-11-05 19:21:49 +00:00
|
|
|
/**
|
2011-10-10 22:22:23 +00:00
|
|
|
* Path property
|
2008-11-05 19:21:49 +00:00
|
|
|
*
|
|
|
|
* Current file's absolute path
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2011-11-18 19:05:30 +00:00
|
|
|
* http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::$path
|
2008-11-05 19:21:49 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = null;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $path Path to file
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $create Create file if it does not exist (if true)
|
|
|
|
* @param int $mode Mode to apply to the folder holding the file
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __construct($path, $create = false, $mode = 0755) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$this->Folder = new Folder(dirname($path), $create, $mode);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_dir($path)) {
|
|
|
|
$this->name = basename($path);
|
|
|
|
}
|
2008-11-05 19:21:49 +00:00
|
|
|
$this->pwd();
|
2010-10-01 03:03:23 +00:00
|
|
|
$create && !$this->exists() && $this->safe($path) && $this->create();
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Closes the current file if it is opened
|
|
|
|
*/
|
2011-05-28 20:38:46 +00:00
|
|
|
public function __destruct() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->close();
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Creates the file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::create
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function create() {
|
2008-05-30 11:40:08 +00:00
|
|
|
$dir = $this->Folder->pwd();
|
|
|
|
if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
if (touch($this->path)) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Opens the current file with a given $mode
|
|
|
|
*
|
|
|
|
* @param string $mode A valid 'fopen' mode string (r|w|a ...)
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $force If true then the file will be re-opened even if its already opened, otherwise it won't
|
|
|
|
* @return bool True on success, false on failure
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::open
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function open($mode = 'r', $force = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$force && is_resource($this->handle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if ($this->exists() === false) {
|
|
|
|
if ($this->create() === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-05 19:21:49 +00:00
|
|
|
$this->handle = fopen($this->path, $mode);
|
2008-05-30 11:40:08 +00:00
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Return the contents of this file as a string.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $bytes where to start
|
2010-01-25 16:01:05 +00:00
|
|
|
* @param string $mode A `fread` compatible mode.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $force If true then the file will be re-opened even if its already opened, otherwise it won't
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed string on success, false on failure
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::read
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function read($bytes = false, $mode = 'rb', $force = false) {
|
2008-10-15 12:42:03 +00:00
|
|
|
if ($bytes === false && $this->lock === null) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return file_get_contents($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-26 22:34:09 +00:00
|
|
|
if ($this->open($mode, $force) === false) {
|
2008-10-15 12:42:03 +00:00
|
|
|
return false;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2008-10-15 12:42:03 +00:00
|
|
|
if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (is_int($bytes)) {
|
|
|
|
return fread($this->handle, $bytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data = '';
|
|
|
|
while (!feof($this->handle)) {
|
|
|
|
$data .= fgets($this->handle, 4096);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->lock !== null) {
|
|
|
|
flock($this->handle, LOCK_UN);
|
|
|
|
}
|
2008-10-15 12:42:03 +00:00
|
|
|
if ($bytes === false) {
|
|
|
|
$this->close();
|
|
|
|
}
|
2010-05-30 15:16:40 +00:00
|
|
|
return trim($data);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Sets or gets the offset for the currently opened file.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int|bool $offset The $offset in bytes to seek. If set to false then the current offset is returned.
|
|
|
|
* @param int $seek PHP Constant SEEK_SET | SEEK_CUR | SEEK_END determining what the $offset is relative to
|
2008-05-30 11:40:08 +00:00
|
|
|
* @return mixed True on success, false on failure (set mode), false on failure or integer offset on success (get mode)
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::offset
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function offset($offset = false, $seek = SEEK_SET) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($offset === false) {
|
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
return ftell($this->handle);
|
|
|
|
}
|
|
|
|
} elseif ($this->open() === true) {
|
|
|
|
return fseek($this->handle, $offset, $seek) === 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2014-07-30 20:11:03 +00:00
|
|
|
* Prepares an ASCII string for writing. Converts line endings to the
|
2013-10-04 06:03:53 +00:00
|
|
|
* correct terminator for the current platform. If Windows, "\r\n" will be used,
|
2010-01-25 16:01:05 +00:00
|
|
|
* all other platforms will use "\n"
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $data Data to prepare for writing.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $forceWindows If true forces usage Windows newline string.
|
2010-01-25 16:01:05 +00:00
|
|
|
* @return string The with converted line endings.
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::prepare
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-12-13 02:02:42 +00:00
|
|
|
public static function prepare($data, $forceWindows = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$lineBreak = "\n";
|
2013-02-12 02:38:08 +00:00
|
|
|
if (DIRECTORY_SEPARATOR === '\\' || $forceWindows === true) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$lineBreak = "\r\n";
|
|
|
|
}
|
|
|
|
return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Write given data to this file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 16:01:05 +00:00
|
|
|
* @param string $data Data to write to this File.
|
|
|
|
* @param string $mode Mode of writing. {@link http://php.net/fwrite See fwrite()}.
|
2016-08-18 17:52:44 +00:00
|
|
|
* @param bool $force Force the file to open
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::write
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function write($data, $mode = 'w', $force = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$success = false;
|
|
|
|
if ($this->open($mode, $force) === true) {
|
2008-10-23 00:10:44 +00:00
|
|
|
if ($this->lock !== null) {
|
|
|
|
if (flock($this->handle, LOCK_EX) === false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite($this->handle, $data) !== false) {
|
|
|
|
$success = true;
|
|
|
|
}
|
|
|
|
if ($this->lock !== null) {
|
|
|
|
flock($this->handle, LOCK_UN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $success;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Append given data string to this file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
|
|
|
* @param string $data Data to write
|
2013-10-04 06:03:53 +00:00
|
|
|
* @param string $force Force the file to open
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::append
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function append($data, $force = false) {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->write($data, 'a', $force);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Closes the current file if it is opened.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if closing was successful or file was already closed, otherwise false
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::close
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function close() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!is_resource($this->handle)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return fclose($this->handle);
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Deletes the file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::delete
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function delete() {
|
2011-08-29 02:19:53 +00:00
|
|
|
if (is_resource($this->handle)) {
|
|
|
|
fclose($this->handle);
|
|
|
|
$this->handle = null;
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return unlink($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file info as an array with the following keys:
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2011-12-19 03:55:37 +00:00
|
|
|
* - dirname
|
|
|
|
* - basename
|
|
|
|
* - extension
|
|
|
|
* - filename
|
|
|
|
* - filesize
|
|
|
|
* - mime
|
|
|
|
*
|
|
|
|
* @return array File information.
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::info
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function info() {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$this->info) {
|
2008-11-05 19:21:49 +00:00
|
|
|
$this->info = pathinfo($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
if (!isset($this->info['filename'])) {
|
|
|
|
$this->info['filename'] = $this->name();
|
|
|
|
}
|
2011-09-18 16:24:56 +00:00
|
|
|
if (!isset($this->info['filesize'])) {
|
|
|
|
$this->info['filesize'] = $this->size();
|
|
|
|
}
|
2011-12-19 03:55:37 +00:00
|
|
|
if (!isset($this->info['mime'])) {
|
|
|
|
$this->info['mime'] = $this->mime();
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->info;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file extension.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-10-04 06:03:53 +00:00
|
|
|
* @return string The file extension
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::ext
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function ext() {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$this->info) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->info();
|
|
|
|
}
|
|
|
|
if (isset($this->info['extension'])) {
|
|
|
|
return $this->info['extension'];
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file name without extension.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-10-04 06:03:53 +00:00
|
|
|
* @return string The file name without extension.
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::name
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function name() {
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$this->info) {
|
2008-05-30 11:40:08 +00:00
|
|
|
$this->info();
|
|
|
|
}
|
|
|
|
if (isset($this->info['extension'])) {
|
2011-12-16 06:52:07 +00:00
|
|
|
return basename($this->name, '.' . $this->info['extension']);
|
2008-05-30 11:40:08 +00:00
|
|
|
} elseif ($this->name) {
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Makes file name safe for saving
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2010-01-25 16:01:05 +00:00
|
|
|
* @param string $name The name of the file to make safe if different from $this->name
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param string $ext The name of the extension to make safe if different from $this->ext
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return string ext The extension of the file
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::safe
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function safe($name = null, $ext = null) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if (!$name) {
|
|
|
|
$name = $this->name;
|
|
|
|
}
|
|
|
|
if (!$ext) {
|
|
|
|
$ext = $this->ext();
|
|
|
|
}
|
2012-05-19 14:48:15 +00:00
|
|
|
return preg_replace("/(?:[^\w\.-]+)/", "_", basename($name, $ext));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Get md5 Checksum of file with previous check of Filesize
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param int|bool $maxsize in MB or true to force
|
2013-10-04 00:40:00 +00:00
|
|
|
* @return string|false md5 Checksum {@link http://php.net/md5_file See md5_file()}, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::md5
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function md5($maxsize = 5) {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($maxsize === true) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return md5_file($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-06-04 21:29:26 +00:00
|
|
|
|
|
|
|
$size = $this->size();
|
|
|
|
if ($size && $size < ($maxsize * 1024) * 1024) {
|
|
|
|
return md5_file($this->path);
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the full path of the file.
|
2009-03-17 21:10:28 +00:00
|
|
|
*
|
2013-10-04 06:03:53 +00:00
|
|
|
* @return string Full path to the file
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::pwd
|
2009-02-04 05:00:59 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function pwd() {
|
2013-08-16 18:12:49 +00:00
|
|
|
if ($this->path === null) {
|
2015-01-11 20:20:34 +00:00
|
|
|
$dir = $this->Folder->pwd();
|
|
|
|
if (is_dir($dir)) {
|
|
|
|
$this->path = $this->Folder->slashTerm($dir) . $this->name;
|
|
|
|
}
|
2008-11-05 19:21:49 +00:00
|
|
|
}
|
|
|
|
return $this->path;
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns true if the file exists.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if it exists, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::exists
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function exists() {
|
2013-02-12 12:58:36 +00:00
|
|
|
$this->clearStatCache();
|
2008-11-05 19:21:49 +00:00
|
|
|
return (file_exists($this->path) && is_file($this->path));
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the "chmod" (permissions) of the file.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2013-10-04 00:40:00 +00:00
|
|
|
* @return string|false Permissions for the file, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::perms
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function perms() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return substr(sprintf('%o', fileperms($this->path)), -4);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file size
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int|false Size of the file in bytes, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::size
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function size() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return filesize($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns true if the file is writable.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if it's writable, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::writable
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function writable() {
|
2008-11-05 19:21:49 +00:00
|
|
|
return is_writable($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the File is executable.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if it's executable, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::executable
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function executable() {
|
2008-11-05 19:21:49 +00:00
|
|
|
return is_executable($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns true if the file is readable.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if file is readable, false otherwise
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::readable
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function readable() {
|
2008-11-05 19:21:49 +00:00
|
|
|
return is_readable($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file's owner.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int|false The file owner, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::owner
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function owner() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return fileowner($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
2013-10-04 06:03:53 +00:00
|
|
|
* Returns the file's group.
|
2008-05-30 11:40:08 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int|false The file group, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::group
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function group() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return filegroup($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns last access time.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int|false Timestamp of last access time, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastAccess
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastAccess() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return fileatime($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
2008-06-20 20:17:23 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns last modified time.
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return int|false Timestamp of last modification, or false in case of an error
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::lastChange
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function lastChange() {
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($this->exists()) {
|
2008-11-05 19:21:49 +00:00
|
|
|
return filemtime($this->path);
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the current folder.
|
|
|
|
*
|
|
|
|
* @return Folder Current folder
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::Folder
|
2008-05-30 11:40:08 +00:00
|
|
|
*/
|
2013-04-15 01:29:04 +00:00
|
|
|
public function folder() {
|
2008-05-30 11:40:08 +00:00
|
|
|
return $this->Folder;
|
|
|
|
}
|
2009-10-09 03:27:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the File to $dest
|
|
|
|
*
|
2013-10-04 06:03:53 +00:00
|
|
|
* @param string $dest Destination for the copy
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $overwrite Overwrite $dest if exists
|
|
|
|
* @return bool Success
|
2011-11-15 06:40:02 +00:00
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::copy
|
2009-10-09 03:27:31 +00:00
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function copy($dest, $overwrite = true) {
|
2009-10-09 03:27:31 +00:00
|
|
|
if (!$this->exists() || is_file($dest) && !$overwrite) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return copy($this->path, $dest);
|
|
|
|
}
|
2011-12-19 03:55:37 +00:00
|
|
|
|
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Get the mime type of the file. Uses the finfo extension if
|
2011-12-19 03:55:37 +00:00
|
|
|
* its available, otherwise falls back to mime_content_type
|
|
|
|
*
|
|
|
|
* @return false|string The mimetype of the file, or false if reading fails.
|
|
|
|
*/
|
|
|
|
public function mime() {
|
2011-12-19 04:26:57 +00:00
|
|
|
if (!$this->exists()) {
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-19 03:55:37 +00:00
|
|
|
if (function_exists('finfo_open')) {
|
|
|
|
$finfo = finfo_open(FILEINFO_MIME);
|
2013-08-07 21:35:19 +00:00
|
|
|
$finfo = finfo_file($finfo, $this->pwd());
|
|
|
|
if (!$finfo) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-11 21:38:24 +00:00
|
|
|
list($type) = explode(';', $finfo);
|
2011-12-19 03:55:37 +00:00
|
|
|
return $type;
|
2013-08-07 21:35:19 +00:00
|
|
|
}
|
|
|
|
if (function_exists('mime_content_type')) {
|
2011-12-19 03:55:37 +00:00
|
|
|
return mime_content_type($this->pwd());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
2013-02-12 12:58:36 +00:00
|
|
|
/**
|
|
|
|
* Clear PHP's internal stat cache
|
|
|
|
*
|
2013-10-04 06:03:53 +00:00
|
|
|
* For 5.3 onwards it's possible to clear cache for just a single file. Passing true
|
2013-02-12 12:58:36 +00:00
|
|
|
* will clear all the stat cache.
|
2013-04-13 18:35:18 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @param bool $all Clear all cache or not
|
2013-02-12 12:58:36 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function clearStatCache($all = false) {
|
|
|
|
if ($all === false && version_compare(PHP_VERSION, '5.3.0') >= 0) {
|
|
|
|
return clearstatcache(true, $this->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clearstatcache();
|
|
|
|
}
|
|
|
|
|
2013-09-21 06:24:14 +00:00
|
|
|
/**
|
2013-10-26 12:38:38 +00:00
|
|
|
* Searches for a given text and replaces the text if found.
|
|
|
|
*
|
|
|
|
* @param string|array $search Text(s) to search for.
|
|
|
|
* @param string|array $replace Text(s) to replace with.
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool Success
|
2013-09-21 06:24:14 +00:00
|
|
|
*/
|
|
|
|
public function replaceText($search, $replace) {
|
2013-10-26 12:38:38 +00:00
|
|
|
if (!$this->open('r+')) {
|
2013-09-21 06:24:14 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->lock !== null) {
|
|
|
|
if (flock($this->handle, LOCK_EX) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 12:38:38 +00:00
|
|
|
$replaced = $this->write(str_replace($search, $replace, $this->read()), 'w', true);
|
2013-09-21 06:24:14 +00:00
|
|
|
|
|
|
|
if ($this->lock !== null) {
|
|
|
|
flock($this->handle, LOCK_UN);
|
|
|
|
}
|
|
|
|
$this->close();
|
|
|
|
|
|
|
|
return $replaced;
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
}
|