How to use the class:
- 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. - Call method
upload()
to upload a CSV file to the temporary files directory. - Call method
getInfo()
to obtain the CSV file summary, i.e. its columns’ titles, contents, and count. - Call method
setFields()
to specify the columns whose contents must be extracted. - Call method
import()
to extract the contents of selected columns.
Methods
-
getDelimiters
Returns the list of delimiters used in CSV files.
-
getFields
Returns the array of columns set via method
setFields()
, whose values must be extracted. -
getInfo
Returns CSV file summary.
-
import
Returns a CSV file columns’ contents.
-
saveContent
Saves text contents to a CSV file.
-
setEncoding
Sets encoding for reading a CSV file.
-
setFields
Sets CSV file columns to read data from.
-
setFile
Sets the name of a CSV in the temporary files directory, to read data from.
-
upload
Uploads a CSV file to the server.
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
-
$limit
Number of lines from the beginning of a CSV file to read data from. Default limit is 50.
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
-
$content
CSV-like contents to be saved to a temporary CSV file.
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
-
$encoding
CSV file character encoding.
Example
$csv = new waCSV(); $csv->setEncoding('windows-1252');
public function setFields ($fields)
Sets CSV file columns to read data from using import()
method.
Parameters
-
$fields
An associative array of columns. Key: zero-based column number; value: column title from the 1st line.
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
-
$file
Name of a CSV file in the temporary files directory.
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
-
$name
Value of
name
attribute of afile
type field of the web form used to upload a CSV file.
Example
$csv = new waCSV(); csv->upload('csv');