Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!defined('BASEPATH'))
- exit('No direct script access allowed');
- final class File {
- private $file;
- private $file_real;
- private $dir_files = array();
- function __construct($file = null) {
- if ($file)
- $this->setFile($file);
- }
- public function __clear() {
- unset($this->file);
- unset($this->file_real);
- unset($this->dir_files);
- }
- public function setFile($file) {
- $this->file = $file;
- $this->file_real = realpath($this->file);
- }
- public function delete($file = null) {
- $this->checkDATA($file);
- $var = unlink($this->file_real) ? true : false;
- $this->__clear();
- return $var;
- }
- public function file_exist($file = null) {
- $this->checkDATA($file);
- return file_exists($this->file_real) ? true : false;
- }
- private function checkDATA($file) {
- if ($file)
- $this->setFile($file);
- if (empty($this->file))
- throw new FileException("\n File class needs a file to work. Use setFile to fix it");
- }
- public function whatIs($file = null) {
- $this->checkDATA($file);
- if (is_file($this->file_real))
- return 'is_file';
- if (is_dir($this->file_real))
- return 'is_directory';
- if (is_link($this->file))
- return 'is_links';
- return false;
- }
- public function realpath($file = null) {
- $this->checkDATA($file);
- //echo $this->file_real;
- return $this->file_real;
- }
- public function length($file = null) {
- return $this->size($file);
- }
- public function size($file = null) {
- $this->checkDATA($file);
- return filesize($this->file_real);
- }
- public function type($file = null) {
- $this->checkDATA($file);
- return filetype($this->file_real);
- }
- public function touched($file = null) {
- $this->checkDATA($file);
- return filemtime($this->file_real);
- }
- public function listFiles($deph = 0, $hiden = false) {
- if ($this->whatIs() != 'is_directory')
- throw new FileException(" You can list only dirs");
- return $this->dir_files = $this->__directory_map($this->file_real, $deph, $hiden);
- }
- public function read_file($file = null) {
- $this->checkDATA($file);
- if (function_exists('file_get_contents')) {
- return file_get_contents($this->file_real);
- }
- if (!$fp = @fopen($this->file_real, FOPEN_READ)) {
- return FALSE;
- }
- flock($fp, LOCK_SH);
- $data = '';
- if (filesize($this->file_real) > 0) {
- $data = & fread($fp, filesize($this->file_real));
- }
- flock($fp, LOCK_UN);
- fclose($fp);
- return $data;
- }
- public function write_file($data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) {
- if (!$fp = fopen($this->file_real, $mode)) {
- return FALSE;
- }
- flock($fp, LOCK_EX);
- fwrite($fp, $data);
- flock($fp, LOCK_UN);
- fclose($fp);
- return TRUE;
- }
- private function __directory_map($source_dir, $directory_depth = 0, $hidden = FALSE) {
- if ($fp = @opendir($source_dir)) {
- $filedata = array();
- $new_depth = $directory_depth - 1;
- $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
- while (FALSE !== ($file = readdir($fp))) {
- // Remove '.', '..', and hidden files [optional]
- if (!trim($file, '.') OR ($hidden == FALSE && $file[0] == '.')) {
- continue;
- }
- if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir . $file)) {
- $filedata[$file] = directory_map($source_dir . $file . DIRECTORY_SEPARATOR, $new_depth, $hidden);
- } else {
- $filedata[] = $file;
- }
- }
- closedir($fp);
- return $filedata;
- }
- return FALSE;
- }
- }
- final class FileException extends Exception {
- public function __construct($message, $code = null, $previous = null) {
- parent::__construct($message, $code, $previous);
- }
- public function __toString() {
- return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement