Lendo e escrevendo em arquivos.
Publicado por Vinicus S Moraes 17/10/2008
[ Hits: 6.201 ]
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 ?>
Abrindo arquivo e convertendo de um caracter para outro
Gerar código de barras com barcode + PHP
Atenção a quem posta conteúdo de dicas, scripts e tal (1)
Manutenção de sistemas Linux Debian e derivados com apt-get, apt, aptitude e dpkg
Melhorando o tempo de boot do Fedora e outras distribuições
Como instalar as extensões Dash To Dock e Hide Top Bar no Gnome 45/46
Instalar a última versão do PostgreSQL no Lunix mantendo atualizado
Flathub na sua distribuição Linux e comandos básicos de gerenciamento
Instalando o FreeOffice no LMDE 6
Anki: Remover Tags de Estilo HTML de Todas as Cartas
Colocando uma opção de redimensionamento de imagem no menu de contexto do KDE
Desenvolvimento de um driver (8)
Não esta salvando as configurações de usuário. (3)
Quais Shell Scripts vocês usam? (5)
[Shell Script] Script para desinstalar pacotes desnecessários no OpenSuse
[Shell Script] Script para criar certificados de forma automatizada no OpenVpn
[Shell Script] Conversor de vídeo com opção de legenda
[C/C++] BRT - Bulk Renaming Tool
[Shell Script] Criação de Usuarios , Grupo e instalação do servidor de arquivos samba