Lendo e escrevendo em arquivos.
Publicado por Vinicus S Moraes 17/10/2008
[ Hits: 6.656 ]
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
Abrindo arquivo e convertendo de um caracter para outro
Página de downloads que identifica arquivos por extensão
Atualizações de Apps, Desktop e Kernel agitam o ecossistema Linux nesta terça-feira
Miyoo Mini Plus + Onion OS (Linux)
IA local no bolso, novo visual no Raspberry Pi OS e mais destaques do software livre
Kernel turbinado, compatibilidade em alta e debate sobre sustentabilidade: o dia no mundo Linux
Kernel turbinado e GNOME 49 dominam o giro do dia no mundo Linux
Adicionando o repositório backports no Debian 13 Trixie
Como definir um IP estático no Linux Debian
Como colocar atalho para uma pasta na área de trabalho do Ubuntu 24.04... (1)
Como listar os arquivos em "bloquin... (0)
Como vencer a procrastinação? (8)
Adicional de convivdado em linux somente linha de comando (3)