Lendo e escrevendo em arquivos.
Publicado por Vinicus S Moraes 17/10/2008
[ Hits: 6.525 ]
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 ?>
Upload de imagens com redimensionamento
Eliminar Todos os sub-diretórios de um Diretório
Upload de arquivos e validação de tipo do mesmo
Como gerar qualquer emoji ou símbolo unicode a partir do seu teclado
Instalar e Configurar o Slackware Linux em 2025
Como configurar os repositórios do apt no Debian 12 em 2025
Passkeys: A Evolução da Autenticação Digital
Instalação de distro Linux em computadores, netbooks, etc, em rede com o Clonezilla
Configurando o Conky para iniciar corretamente no sistema
3 configurações básicas que podem melhorar muito a sua edição pelo editor nano
Como colorir os logs do terminal com ccze
Instalação Microsoft Edge no Linux Mint 22
Como configurar posicionamento e movimento de janelas no Lubuntu (Openbox) com atalhos de teclado
firefox nao guarda meus logins nos sites (1)
Instalar debian testing (13) "por cima" do debian 12 (2)
Erro de segmentação «Segmentation fault (core dumped)» ao retornar obj... (1)