12

How do I import CSV files using zend framework? Should I use zend_file_transfer or is there any special class that I have to look into? Also if I use zend_file_transfer is there any special validator for CSV?

JABD
  • 640
  • 2
  • 10
  • 23
  • 3
    As far as I'm aware, there's no class to parse CSV in Zend (unless you're looking to parse a translation file with Zend_Translate or if you're using Zend_Auth with CSV files). It's pretty straightforward with `fgetcsv` so I guess there's no need for a class there. – netcoder Jun 13 '11 at 18:43

4 Answers4

13

you don't have to use any zend libraries to import csv files, you can just use native php functions, take a look at fgetcsv

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
  • 2
    Side warning: `fgetcsv()` doesn't really support unicode, so you might need to juggle locales. – Darien Jun 13 '11 at 18:18
  • yeah, I know how to do it using fgetcsv but I am specifically interested via zend. Thanks for your reply anyways. – JABD Jun 13 '11 at 18:18
  • Built in CSV functions are flawed and buggy. It is a nightmare to try to parse CSV in native PHP. – Josef Sábl May 20 '13 at 15:34
9

You could also use SplFileObject for reading CSV files.

From the php manual:

<?php
    $file = new SplFileObject("animals.csv");
    $file->setFlags(SplFileObject::READ_CSV);
    foreach ($file as $row) {
        list($animal, $class, $legs) = $row;
        printf("A %s is a %s with %d legs\n", $animal, $class, $legs);
    }
?> 

http://php.net/manual/en/splfileobject.fgetcsv.php

wickedone
  • 542
  • 1
  • 6
  • 18
  • 1
    SplFileObject doesn't provide any useful validation/errors -- passing in rubbish will just result in $row being an array of one element where the element is the entire line. – David Goodwin Nov 27 '14 at 13:52
1

There is currently no way to do this with the Zend Framework. How can one be sure?

For example, Zend_Translate supports translation with CSV files, but if you check the the source code of the respective adapter (Zend_Translate_Adapter_Csv), you can verify it uses fgetcsv, and not a specific Zend class. Besides, this CSV adapter comes with the following warning:

Note: Beware that the Csv Adapter has problems when your Csv files are encoded differently than the locale setting of your environment. This is due to a Bug of PHP itself which will not be fixed before PHP 6.0 (http://bugs.php.net/bug.php?id=38471). So you should be aware that the Csv Adapter due to PHP restrictions is not locale aware.

which is related with the problems of the fgetcsv function.

faken
  • 6,572
  • 4
  • 27
  • 28
0

Here's a function that reads a csv file and returns an array of items that contain the first two column data values.

This function could read a file of first_name,last_name for example.

function processFile ($filename) {
    $rtn = array();

    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $item = array();
            $item[] = $data[0];
            $item[] = $data[1];
            $rtn[] = $item;
        }
    }
    return $rtn;
 }
Brad Lucas
  • 185
  • 2
  • 6