Lendo e escrevendo em arquivos.
Publicado por Vinicus S Moraes 17/10/2008
[ Hits: 6.574 ]
Homepage: http://vsmoraes.wordpress.com
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.
<?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 ?>
Conversor de arquivo Excel para um Array
Classe para envio de arquivo - Programação Orientada a Objetos
Aprenda a Gerenciar Permissões de Arquivos no Linux
Como transformar um áudio em vídeo com efeito de forma de onda (wave form)
Como aprovar Pull Requests em seu repositório Github via linha de comando
Aplicativo simples para gravar tela
Quebra de linha na data e hora no Linux Mint
Firefox não abre em usuário não administradores (0)
Sempre que vou baixar algum pacote acontece o erro dpkg (8)
tentando instalar em um notebook antigo o Linux LegacyOS_2023... [RESO... (8)
Problema com Conexão Outlook via Firewall (OpenSUSE) com Internet Fibr... (5)