Lendo e escrevendo em arquivos.

Publicado por Vinicus S Moraes 17/10/2008

[ Hits: 6.338 ]

Homepage: http://vsmoraes.wordpress.com

Download file.class.php




Uma classe que eu fiz, com verificação de permissões e montagem de strings com zeros ou espaços complementares.
Espero que gostem.

OBS: A classe não está sob nenhuma licença, então é livre pra distribuição, alteração, etc.

  



Esconder código-fonte

<?php
   class myFile {
      private $_PATH;
      private $_MODE;
      private $_HANDLE;
      private $_LINE;

      public function __construct($path, $mode) {
         $this->_PATH = $path;
         $this->_MODE = $mode;

         if ( $this->_PATH && $this->_MODE ) {
            $this->__verifypermissions();
         }
      } // __construct

      public function __destruct() {
         if ( $this->_HANDLE ) {
            @fclose($this->_PATH);
         }
      } // __destruct

      private function __throwerror($code) {
         $errors = array(
               // File permissions
               "0x001" => "File don't exist. Use 'f' to force the class to create the file.",
               "0x002" => "You don't have rights to read the file.",
               "0x003" => "You don't have rights to write in the file.",
               "0x004" => "Can't create the file, verify your rights.",
               // Options verification
               "1x001" => "File not set.",
               "1x002" => "Mode not set.",
               // Modes verification
               "2x001" => "Wrong mode. Must inform read or write mode.",
               "2x002" => "You can't read a write-only file.",
               "2x003" => "You can't write a read-only file.");

         printf("<strong>File Class Error.</strong><br />\n");
         printf("<strong>Error code:</strong> %s <br />\n", $code);
         printf("<strong>Error Message:</strong> %s <br /><br />\n", $errors[$code]);
         die();
      } // __throwerror

      private function __verifypermissions() {
         if ( !isset($this->_PATH) || empty($this->_PATH) ) {
            $this->__throwerror("1x001");
            return false;
         } elseif ( !isset($this->_MODE) || empty($this->_MODE) ) {
            $this->__throwerror("1x002");
            return false;
         } else {
            if ( !ereg("[rw]", $this->_MODE) ) {
               $this->__throwerror("2x001");
               return false;
            } else {
               if ( ereg("r", $this->_MODE) && ereg("w", $this->_MODE) ) {
                  if ( file_exists($this->_PATH) ) {
                     if ( !$this->_HANDLE = @fopen($this->_PATH, "rw") ) {
                        $this->__throwerror("0x004");
                        return false;
                     } else {
                        return true;
                     }
                  } else {
                     if ( ereg("f", $this->_MODE) ) {
                        if ( !$this->_HANDLE = @fopen($this->_PATH, "a+") ) {
                           $this->__throwerror("0x004");
                           return false;
                        } else {
                           return true;
                        }
                     } else {
                        $this->__throwerror("0x001");
                        return false;
                     }
                  }
               } elseif ( ereg("r", $this->_MODE) ) {
                  if ( !$this->_HANDLE = @fopen($this->_PATH, "r") ) {
                     $this->__throwerror("0x002");
                     return false;
                  } else {
                     return true;
                  }
               } elseif ( ereg("w", $this->_MODE) ) {
                  if ( file_exists($this->_PATH) ) {
                     if ( !$this->_HANDLE = @fopen($this->_PATH, "w") ) {
                        $this->__throwerror("0x003");
                        return false;
                     } else {
                        return true;
                     }
                  } else {
                     if ( ereg("f", $this->_MODE) ) {
                        if ( !$this->_HANDLE = @fopen($this->_PATH, "a") ) {
                           $this->__throwerror("0x004");
                           return false;
                        } else {
                           return true;
                        }
                     } else {
                        $this->__throwerror("0x001");
                        return false;
                     }
                  }
               }
            }
         }
      } // __verifypermissions

      private function __isopen() {
         if ( !$this->_HANDLE ) {
            if ( $this->__verifypermissions() ) {
               return true;
            } else {
               return false;
            }
         } else {
            return true;
         }
      } // __isopen

      public function __readline() {
         if ( $this->__isopen() ) {
            if ( !ereg("r", $this->_MODE) ) {
               $this->__trhowerror("2x003");
               return false;
            } else {
               $this->_LINE = fread($this->_HANDLE, 8192);
               return $this->_LINE;
            }
         } else {
            return false;
         }
      } // __readline

      public function __writeline($str) {
         if ( $this->__isopen() ) {
            if ( !ereg("w", $this->_MODE) ) {
               $this->__throwerror("2x003");
               return false;
            } else {
               fwrite($this->_HANDLE, $str."\n");
               return true;
            }
         } else {
            return false;
         }
      } // __writeline

      private function __returnstr($str, $size) {
         if ( is_string($tmp) ) {
            $tmp = $str;
            while ( strlen($tmp) < $size ) {
               $tmp .= " ";
            }
            return $tmp;
         } else {
            return false;
         }
      } // __returnstr

      private function __returnint($int, $size) {
         if ( is_int($int) ) {
            $tmp = "";
            while ( strlen($tmp) < ($size-strlen($int)) ) {
               $tmp .= "0";
            }
            $tmp .= $int;
            return $tmp;
         } else {
            return false;
         }
      } // __returnint

      public function __mountline($array) {
         $str = "";
         for ( $i = 0; $i < count($array); $i++ ) {
            if ( $array[$i]["type"] == "str" ) {
               $str .= $this->__returnstr($array[$i]["value"], $array[$i]["size"]);
            } elseif ( $array[$i]["type"] == "int" ) {
               $str .= $this->__returnint($array[$i]["value"], $array[$i]["size"]);
            }
         }

         if ( $this->__writeline($tmp) ) {
            return $tmp;
         }
      } // __mountline

   }; // File
?>

Scripts recomendados

Upload de imagens com redimensionamento

Eliminar Todos os Ficheiros de um Directorio

Upload de arquivos

Gerando vários arquivos com PHP usando MPDF

Image Class


  

Comentários
[1] Comentário enviado por gpr.ppg.br em 18/10/2008 - 02:17h

hujhjh


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts