0

This Code works fine:

$csvType = $_FILES['csvtype']['tmp_name']['csv-sparkasse'];

function handleCsv($em, $csvType){
    $handle = fopen($csvType, "r");
            $i = 0;
            while (($fileop = fgetcsv($handle, 1000, ";")) !== false) {
                $entity = new Sparkasse();
                $entity->setAuftragskonto($fileop[0]);
                $entity->setBuchungstag($fileop[1]);
                $entity->setValutadatum($fileop[2]);
                $entity->setBuchungstext($fileop[3]);
                $entity->setVerwendungszweck($fileop[4]);
                $entity->setZahlungspflichtiger($fileop[5]);
                $entity->setKontonummer($fileop[6]);
                $entity->setBlz($fileop[7]);
                $entity->setBetrag($fileop[8]);
                $entity->setWaehrung($fileop[9]);
                $entity->setInfo($fileop[10]);

                if ($i > 0) {
                    $em->persist($entity);
                }
                $i++;
            }

            $em->flush();

            /* Update Table Sparkasse and create csv */
            $connection = $em->getConnection();
            $sql = $connection->prepare("UPDATE Sparkasse,Rechnung SET                 Sparkasse.Rechnungsnr = Rechnung.Belegnr
                WHERE Sparkasse.Betrag = Rechnung.Gesamt AND 
                (Sparkasse.Verwendungszweck LIKE CONCAT('%',Rechnung.Belegnr,'%') OR 
                UPPER(Sparkasse.Zahlungspflichtiger) LIKE UPPER(Rechnung.Name) OR 
                UPPER(Sparkasse.Zahlungspflichtiger) = UPPER(Rechnung.Firma) OR 
                Sparkasse.Verwendungszweck LIKE CONCAT('%',Rechnung.Kundennr,'%'))");
            $sql->execute();

            $query = $em->createQuery("SELECT s FROM AdminBundle:Sparkasse s");
            $tableSparkasse = $query->getArrayResult();
            $this->createCsv($tableSparkasse, 'sparkasse.csv');
}

And here is the problem. The csv-file was downloaded on the browser and the table is correct. But at the end of the file you can see the php code of the current page.

function createCsv($rows, $filename) {
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=' . $filename);

    $fp = fopen('php://output', 'w');

    foreach ($rows as $fields) {
        fputcsv($fp, $fields, ';', '"');
    }

    fclose($fp);
}

Here you can see the file: https://dl.dropboxusercontent.com/u/6242507/sparkasse.csv

Rico
  • 58,485
  • 12
  • 111
  • 141
chuebert
  • 136
  • 1
  • 6
  • The page that emits the CSV file for download is emitting some HTML after the file has been sent. The page should stop immediately after sending the file. –  Feb 07 '14 at 01:12
  • This has nothing todo with symfony? Or are you creating this in a symfony controller action? – Emii Khaos Feb 07 '14 at 09:20
  • after the while add if(is_numeric($fileop[0])) {....} – kraysak Feb 08 '14 at 00:39

1 Answers1

0

Thanks for the answer. The first answer gives me the right idea. after die fclose(); I test a die; and it works fine.

In my Controller-Action i return the form:

return $this->render('AdminBundle:Default:index.html.twig', array(
    'form' => $form->createView(),
));

i replaced it with:

return 0;

it works fine. But i dont understand why php write at the end of the file the return after closing with

fclose();
chuebert
  • 136
  • 1
  • 6