waCSV

Uploading & processing of CSV files

Contents...

How to use the class:

  1. Create an instance of waCSV class in a PHP controller processing POST requests for uploading CSV files. An uploaded CSV file must be accessible in global $_FILES array.
  2. Call method upload() to upload a CSV file to the temporary files directory.
  3. Call method getInfo() to obtain the CSV file summary, i.e. its columns’ titles, contents, and count.
  4. Call method setFields() to specify the columns whose contents must be extracted.
  5. Call method import() to extract the contents of selected columns.

Methods

public static function getDelimiters()

Returns the list of delimiters used in CSV files.

Example

waCSV::getDelimiters()

Result

array(
  0 => array(
    0 => ',',
    1 => 'Comma',
  ),
  1 => array(
    0 => ';',
    1 => 'Semicolon',
  ),
  2 => array(
    0 => '	',
    1 => 'Tab',
  ),
)

public function getFields()

Returns the array of columns set via method setFields(), whose values must be extracted.

Example

$csv = new waCSV();
$csv->setFields(array(
    0 => 'Name',
    3 => 'Currency',
));
$fields = $csv->getFields();

Result

array(
    0 => 'Name',
    3 => 'Currency',
)

public function getInfo()

Returns CSV file summary: delimiter, encoding, and columns’ titles, contents, and count.

Example

$csv = new waCSV(false, ';');
$csv->upload('csv');
$info = $csv->getInfo();

Result

array(
    'delimiter' => ';',
    'encode' => false,
    'fields' => array(
        0 => 'Name',
        1 => 'Description',
        2 => 'Price',
        3 => 'Currency',
    ),
    'records' => array(
        0 => array(
            0 => '...',
            1 => '...',
            2 => '...',
            3 => '...',
        ),
        1 => array(
            0 => '...',
            1 => '...',
            2 => '...',
            3 => '...',
        ),
        ...
    ),
    'count' => 38,
)

public function import ($limit = 50)

Returns the contents of a CSV file, from columns set in class field fields using setFields() method. If class field fields is empty, the method returns the contents of all columns in a file.

Parameters

Example

$csv = new waCSV();
$csv->upload('csv');
$csv->setFields(array(
    0 => 'Name',
    2 => 'Price',
    3 => 'Currency',
));
$data = $csv->import();

Result

array(
  1 => array(
    'Name' => 'Product 1',
    'Price' => '699',
    'Currency' => 'USD',
  ),
  2 => array(
    'Name' => 'Product 2',
    'Price' => '799',
    'Currency' => 'USD',
  ),
  ...
)

public function saveContent ($content)

Saves text contents to a CSV file. Useful for importing CSV-like data from a different type of source than a CSV file; e.g., from a POST request.

Parameters

Example

$csv = new waCSV();
$csv_content = waRequest::post('csv');
$csv->saveContent($csv_content);
$data = $csv->import();

public function setEncoding ($encoding)

Sets encoding for reading a CSV file. By default, character encoding UTF-8 is used to read CSV files. If a file is saved with a different encoding, set it before read a file.

Parameters

Example

$csv = new waCSV();
$csv->setEncoding('windows-1252');

public function setFields ($fields)

Sets CSV file columns to read data from using import() method.

Parameters

Example

$csv = new waCSV();
$csv->upload('csv');
$csv->setFields(array(
    0 => 'Name',
    2 => 'Price',
    3 => 'Currency',
));
$data = $csv->import();

public function setFile ($file)

Sets the name of a CSV in the temporary files directory, to read data from. Useful for alternate actions with multiple CSV files.

Parameters

Example

$csv = new waCSV();
$post_csv = waRequest::post('csv');
$uploaded_as_text = $csv->saveContent($post_csv);
$uploaded_as_file = csv->upload('csv');

$csv->setFile($uploaded_as_text);
$data_from_text = $csv->import();

$csv->setFile($uploaded_as_file);
$data_from_file = $csv->import();

public function upload ($name)

Uploads a CSV file to the server.

Parameters

Example

$csv = new waCSV();
csv->upload('csv');