Advertisement
pisio

PHP FILE CLASS BY JAVA

Feb 18th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | None | 0 0
  1. <?php
  2.  
  3. if (!defined('BASEPATH'))
  4.     exit('No direct script access allowed');
  5.  
  6. final class File {
  7.  
  8.     private $file;
  9.     private $file_real;
  10.     private $dir_files = array();
  11.  
  12.     function __construct($file = null) {
  13.         if ($file)
  14.             $this->setFile($file);
  15.     }
  16.  
  17.     public function __clear() {
  18.         unset($this->file);
  19.         unset($this->file_real);
  20.         unset($this->dir_files);
  21.     }
  22.  
  23.     public function setFile($file) {
  24.         $this->file = $file;
  25.         $this->file_real = realpath($this->file);
  26.     }
  27.  
  28.     public function delete($file = null) {
  29.         $this->checkDATA($file);
  30.         $var = unlink($this->file_real) ? true : false;
  31.         $this->__clear();
  32.         return $var;
  33.     }
  34.  
  35.     public function file_exist($file = null) {
  36.         $this->checkDATA($file);
  37.  
  38.         return file_exists($this->file_real) ? true : false;
  39.     }
  40.  
  41.     private function checkDATA($file) {
  42.         if ($file)
  43.             $this->setFile($file);
  44.         if (empty($this->file))
  45.             throw new FileException("\n File class needs  a file to work. Use setFile to fix it");
  46.     }
  47.  
  48.     public function whatIs($file = null) {
  49.         $this->checkDATA($file);
  50.         if (is_file($this->file_real))
  51.             return 'is_file';
  52.         if (is_dir($this->file_real))
  53.             return 'is_directory';
  54.         if (is_link($this->file))
  55.             return 'is_links';
  56.  
  57.         return false;
  58.     }
  59.  
  60.     public function realpath($file = null) {
  61.         $this->checkDATA($file);
  62.         //echo $this->file_real;
  63.         return $this->file_real;
  64.     }
  65.  
  66.     public function length($file = null) {
  67.         return $this->size($file);
  68.     }
  69.  
  70.     public function size($file = null) {
  71.         $this->checkDATA($file);
  72.         return filesize($this->file_real);
  73.     }
  74.  
  75.     public function type($file = null) {
  76.         $this->checkDATA($file);
  77.         return filetype($this->file_real);
  78.     }
  79.  
  80.     public function touched($file = null) {
  81.         $this->checkDATA($file);
  82.         return filemtime($this->file_real);
  83.     }
  84.  
  85.     public function listFiles($deph = 0, $hiden = false) {
  86.         if ($this->whatIs() != 'is_directory')
  87.             throw new FileException(" You can list only  dirs");
  88.         return $this->dir_files = $this->__directory_map($this->file_real, $deph, $hiden);
  89.     }
  90.  
  91.     public function read_file($file = null) {
  92.         $this->checkDATA($file);
  93.  
  94.         if (function_exists('file_get_contents')) {
  95.             return file_get_contents($this->file_real);
  96.         }
  97.  
  98.         if (!$fp = @fopen($this->file_real, FOPEN_READ)) {
  99.             return FALSE;
  100.         }
  101.  
  102.         flock($fp, LOCK_SH);
  103.  
  104.         $data = '';
  105.         if (filesize($this->file_real) > 0) {
  106.             $data = & fread($fp, filesize($this->file_real));
  107.         }
  108.  
  109.         flock($fp, LOCK_UN);
  110.         fclose($fp);
  111.  
  112.         return $data;
  113.     }
  114.  
  115.     public function write_file($data, $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE) {
  116.         if (!$fp = fopen($this->file_real, $mode)) {
  117.             return FALSE;
  118.         }
  119.  
  120.         flock($fp, LOCK_EX);
  121.         fwrite($fp, $data);
  122.         flock($fp, LOCK_UN);
  123.         fclose($fp);
  124.  
  125.         return TRUE;
  126.     }
  127.  
  128.     private function __directory_map($source_dir, $directory_depth = 0, $hidden = FALSE) {
  129.         if ($fp = @opendir($source_dir)) {
  130.             $filedata = array();
  131.             $new_depth = $directory_depth - 1;
  132.             $source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  133.  
  134.             while (FALSE !== ($file = readdir($fp))) {
  135.                 // Remove '.', '..', and hidden files [optional]
  136.                 if (!trim($file, '.') OR ($hidden == FALSE && $file[0] == '.')) {
  137.                     continue;
  138.                 }
  139.  
  140.                 if (($directory_depth < 1 OR $new_depth > 0) && @is_dir($source_dir . $file)) {
  141.                     $filedata[$file] = directory_map($source_dir . $file . DIRECTORY_SEPARATOR, $new_depth, $hidden);
  142.                 } else {
  143.                     $filedata[] = $file;
  144.                 }
  145.             }
  146.  
  147.             closedir($fp);
  148.             return $filedata;
  149.         }
  150.  
  151.         return FALSE;
  152.     }
  153.  
  154. }
  155.  
  156. final class FileException extends Exception {
  157.  
  158.     public function __construct($message, $code = null, $previous = null) {
  159.         parent::__construct($message, $code, $previous);
  160.     }
  161.  
  162.     public function __toString() {
  163.         return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
  164.     }
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement