Skip to content

Instantly share code, notes, and snippets.

@andrebian
Last active December 17, 2015 10:38
Show Gist options
  • Save andrebian/5595742 to your computer and use it in GitHub Desktop.
Save andrebian/5595742 to your computer and use it in GitHub Desktop.
Leitura de arquivo
<?php
class Clientes {
public function upload_arquivo() {
if ( !empty($_FILES['arquivo']['tmp_name']) ) {
$status = true;
move_uploaded_file($_FILES['arquivo']['tmp_name'], '../wp-content/uploads/Clientes.txt');
$arquivo = file('../wp-content/uploads/Clientes.txt');
$this->__remover_clientes_antigos();
foreach($arquivo as $linha) {
$dados_linha = explode(';', $linha);
if (! $this->__grava_cliente($dados_linha[1], $dados_linha[2], $dados_linha[3], $dados_linha[4]) ) {
$status = false;
}
}
return true;
return $status;
} else {
return false;
}
}
private function __remover_clientes_antigos() {
@mysql_query("TRUNCATE wp_clientes");
}
private function __grava_cliente($uf, $cidade, $cliente, $produto) {
@mysql_query("
INSERT INTO wp_clientes SET uf='".$this->trocaCaracteres($uf, 'html')."', cidade='".$this->trocaCaracteres($cidade, 'html')."', cliente='".$this->trocaCaracteres($cliente, 'html')."', produto='".$produto."'
");
//echo "INSERT INTO wp_clientes SET uf='".$this->trocaCaracteres($uf, 'html')."', cidade='".$this->trocaCaracteres($cidade)."', cliente='".$this->trocaCaracteres($cliente)."', produto='".$produto."'<br />";
if (mysql_affected_rows() && !mysql_error() ) {
return true;
}
}
public function trocaCaracteres($string, $tipo = 'plain') {
$trocaIsso = array('á', 'à', 'â', 'ã', 'ª', 'Á', 'À', 'Â', 'Ã', 'É', 'È', 'Ê', 'é', 'è', 'ê', 'í', 'ì', 'Í', 'Ì', 'Ó', 'Ò', 'Ô', 'Õ', 'ó', 'ò', 'ô', 'õ', 'º', 'Ú', 'Ù', 'Û', 'ú', 'ù', 'û', 'ç', 'Ç');
$porIsso = array('a', 'a', 'a', 'a', 'a', 'A', 'A', 'A', 'A', 'E', 'E', 'E', 'e', 'e', 'e', 'i', 'i', 'I', 'I', 'O', 'O', 'O', 'O', 'o', 'o', 'o', 'o','o','U', 'U', 'U', 'u', 'u', 'u', 'c', 'C');
$porIssoHtml = array('&aacute;', '&agrave;', '&acirc;', '&atilde;', '&ordf;', '&Aacute;', '&Agrave;', '&Acirc;', '&Atilde;', '&Eacute;', '&Egrave;', '&Ecirc;', '&eacute;', '&egrave;', '&ecirc;', '&iacute;', '&igrave;', '&Iacute;', '&Igrave;', '&Oacute;', '&Ograve;', '&Ocirc;', '&Otilde;', '&oacute;', '&ograve;', '&ocirc;', '&otilde;', '&ordm;', '&Uacute;', 'U', 'U', '&uacute;', 'u', 'u', '&ccedil;', '&Ccedil;');
if ($tipo == 'plain') {
$string = str_replace($trocaIsso, $porIsso, $string);
} else {
$string = str_replace($trocaIsso, $porIssoHtml, $string);
}
return $string;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment