2

I am using this class:

<?php

class csv{
private $separador;
private $cabecalho;
private $dados;
private $path;
private $arquivo;

public function csv($separador=null, $cabecalho=null, $dados="", $path="", $arquivo="csv"){
    #seta as propriedades
    $this->separador = $separador;
    $this->cabecalho = $cabecalho;
    $this->dados = $dados;
    $this->path = $path;
    $this->arquivo = $arquivo;
}
public function salvar(){
    #gera string de cabeçalho
    $colunas = "";
        foreach($this->cabecalho as $coluna){
            if ($colunas == ""){
            $colunas .= $coluna;
            } else {
            $colunas .= $this->separador.$coluna;
            }
        }
    $saida[] = $colunas;
    #gera string do corpo do arquivo
    foreach ($this->dados as $linha){
        #pega as variaveis do array
        $colunasDados = "";
        foreach($linha as $coluna){
            if ($colunasDados == ""){
            $colunasDados .= $coluna;
            } else {
            $colunasDados .= $this->separador.$coluna;
            }
        }
        $saida[] = $colunasDados;
}

    #verifica se alguma linha foi inserida
    if(count($saida)>1){
        #monta o corpo do CSV
        $corpo = implode("\n", $saida);
        #abre um arquivo para escrita, se o arquivo não existir ele tenta criar
        $fp = fopen ($this->path.$this->arquivo.".csv", "w");// W = sobrescreve
        if($fp <> NULL){
            #escreve no arquivo
            fwrite($fp, $corpo);
            #fecha o arquivo
            fclose($fp);
            #retorno do sistema
            echo "<p>Pronto</p>";
                        } 
            else {
            echo "<p>Verifique se a pasta ou o arquivo tem permissão para escrita!</p>";
                    }
     } 
      else {
        echo "<p>Sem linhas para importação!</p>";
            }
    }

to export the csv file.

And this is mine file that rotates:

 <?php
#chama classe
include("program.class.php");
$objeto = new csv();
#Executa os selects         
$tijuca = $objeto->selecionaTijuca();
#gera cabeçalho
$cabecalho = array("mes","ano","titulobanco","matricula");
#cria instancia de objeto da classe
$tijuca = new CSV (";", $cabecalho, $tijuca,"pasta/Nova/", "cotas");
$tijuca->salvar();

?>

I am having this problem of memory:

Fatal error: Allowed memory size of 524288000 bytes exhausted (tried to allocate 262144 bytes) in .../program.class.php on line 42

and I've seen some solutions who have the ability to edit the php memory with this command:

ini_set("memory_limit","500M");

But it does not solve, and overloads my server. Anyone know how do I export a csv file without overloading? or have something in my program what is wrong?

Sorry for my english.

Thank you

2
  • try to create the file and then append data to it line by line, i think that the fatal error is being displayed because of the amount of data that you are holding the array $saida. Commented Jan 14, 2014 at 18:41
  • can you show me a example? Commented Jan 14, 2014 at 18:43

1 Answer 1

1

Take a look at the postgresql copy command. It has an option to output CSV directly.

Sign up to request clarification or add additional context in comments.

3 Comments

The COPY command is quite hard to use via a language database binding. In this case, I recommend using PHP's support for it, php.net/manual/en/function.pg-copy-to.php
pg_copy_to fell into disuse, but i try to use and i have this mesage: pg_copy_to() [function.pg-copy-to]: Copy command failed: ERROR: cannot copy from view "table" HINT: Try the COPY (SELECT ...) TO variant. in
You can't use it on a view, but you can use it on a subquery, so where you would say "myview" you would say "(SELECT * FROM myview)".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.