Advertisement
lamhottt

Class File Text Generator PHP

Aug 10th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.47 KB | None | 0 0
  1. // Class Untuk Membuat File Text dan Membaca File Text
  2. // Masukkan kedalam file "system/GF_Text.php"    
  3. class File_Generator
  4.       {
  5.         private $content_data;
  6.         private $file_name;
  7.         private $message_die = "Unable to open file !";
  8.         private $length_file = false;
  9.  
  10.  
  11.         public function setData($content)
  12.         {
  13.           if ($content)
  14.           {
  15.             $this->content_data = $content;
  16.           }
  17.         }
  18.  
  19.         public function setMessageDie($value = "")
  20.         {
  21.           $this->message_die = $value;
  22.         }
  23.  
  24.         public function setFile($value = 'default.txt')
  25.         {
  26.             $this->file_name = $value;
  27.         }
  28.  
  29.         public function create()
  30.         {
  31.           if (is_array($this->file_name))
  32.             {
  33.               for ($i=0; $i < count($this->file_name) ; $i++)
  34.               {  
  35.                     $fn =  __STORAGE_DIR__.$this->file_name[$i] ;
  36.  
  37.                     if (strpos($fn, '/') !== false)
  38.                     {
  39.                         if (! is_dir(dirname($fn,1)))
  40.                         {
  41.                           mkdir(dirname($fn,1), 0777, true);
  42.                         }
  43.                     }
  44.                     $file   = fopen($fn, "w") or die($this->message_die." : ".$this->file_name);
  45.                     fwrite($file, $this->content_data);
  46.                     fclose($file);
  47.               }
  48.             }
  49.             else
  50.             {
  51.                 $file   = fopen(__STORAGE_DIR__.$this->file_name, "w") or die($this->message_die);
  52.                 fwrite($file, $this->content_data);
  53.                 fclose($file);
  54.             }
  55.         }
  56.  
  57.         public function read()
  58.         {
  59.             if (file_exists(__STORAGE_DIR__.$this->file_name))
  60.             {
  61.                 $file = fopen(__STORAGE_DIR__.$this->file_name, "r") or die($this->message_die." : ".$this->file_name);
  62.                 if ( ($this->length_file != false ) && is_numeric($this->length_file))
  63.                 {
  64.                   return fgets($file,$this->length_file);
  65.                 }
  66.                 else
  67.                 {
  68.                   return fgets($file);
  69.                 }
  70.                 fclose($file);
  71.             }
  72.             else
  73.             {
  74.               return false;
  75.             }
  76.         }
  77.  
  78.         public function setLength($value)
  79.         {
  80.           if (is_numeric($value))
  81.           {
  82.             $this->length_file = $value;
  83.           }
  84.         }
  85.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement