Advertisement
Guest User

csv.php

a guest
Aug 29th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2.  
  3.      class CSV
  4.      {
  5.          private $filename ;
  6.          private $lines ;
  7.  
  8.          function __construct($filename)
  9.          {
  10.              $this->filename = $filename;
  11.              $this->LoadFile();
  12.          }
  13.  
  14.         private function LoadFile()
  15.         {
  16.             if (file_exists($this->filename) && filesize($this->filename) > 0 && $fp = @fopen($this->filename, 'r'))
  17.                 $this->lines = explode("\n", fread($fp, filesize($this->filename)));
  18.             else
  19.                 echo ("file not found");
  20.         }
  21.        
  22.          public function GetLast()
  23.          {
  24.             if ($this->Count() > 0)
  25.                 return $this->lines[$this->Count() - 1];
  26.             else
  27.                 return NULL;
  28.          }
  29.  
  30.          public function DeleteLast()
  31.          {
  32.             if (is_array($this->lines) && $this->Count() > 0)
  33.                 array_pop($this->lines);
  34.          }
  35.          
  36.          public function Save()
  37.          {
  38.             if ($fp = @fopen($this->filename, 'w'))
  39.             {
  40.                 for ($i = 0 ; $i < $this->Count() ; $i++)
  41.                     fwrite($fp, $this->lines[$i] . ($i == $this->Count()-1 ? "" : "\n"));
  42.  
  43.                 fclose($fp);
  44.             }
  45.            
  46.          }
  47.        
  48.         public function Count()
  49.         {
  50.             if (is_array($this->lines))
  51.                 return count($this->lines);
  52.             else
  53.                 return 0;
  54.         }
  55.  
  56.      }
  57.  
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement