In this tutorial I will explain clearly, practically and with functions like creating CSV files in PHP.
This task is really easy because PHP has functions directed for this purpose.
In this short programming article you will find:
- Native PHP functions for writing and reading CSV files.
- How to read a CSV format file step by step.
- How to write a CSV file.
- Copy and paste function that converts from CSV to ARRAY.
- PHP copy-paste function that passes ARRAY to CSV.
Content
- PHP functions to read and write CSVs
- How to read a CSV file
- How to write/create a CSV
- Function passing CSV to Array
- Function passing Array to CSV
PHP functions to read and write CSVs
To read and write a CSV in PHP we need to use 2 PHP functions specially designed for this purpose. The fgetcsv function and the fputcsv function.
The fputcsv function is capable of converting an array into a csv line while the fgetcsv function is capable of performing the opposite process, that is, passing a line from a file to an array with the different values of its columns.
The F’sfputcsv and fgetcsv functions They are configured by default so that the column separator is a comma, and the quotes the “enclosure” or what is the same, the character that is used to contain the texts of the columns that contain the separator character.
If the default configuration of these functions does not adapt to your read file, you can configure them using parameter 3 and 4 of the function.
For example, to read a file that is separated by a semicolon character and the text character is a single quote, you could configure the function as follows:
fgetcsv( $fichero, 0, ";", "'"); //para insertar en el fichero sería fputcsdv( $fichero, $arrayLinea, ";", "'");
How to read a CSV file
As I mentioned in the previous point, to read a CSV extension file effectively in PHP we mainly need a function: fgetcsv. But not only with this function we will be able to read a complete CSV file.
To read a CSV file we will also use the common file manipulation functions: fopen, feof and fclose. The complete process for reading a CSV file is as follows:
- Open the CSV extension file that we want to read.
- Go through the file through a loop and the function to know if the file has finished feof.
- Extract each line from the file using fgetcsv
- Close the file in question with close.
With this scheme, create a function or PHP code that reads the file and ends up passing it to an array to facilitate its reading later. At least this is the way I usually prefer to go through them, you could also simply print their content on the screen, or work with the extracted data in some specific way.
The following code would perform the reading that I just explained to you and store the content in the $data variable.
$file = fopen( 'ruta-fichero/fichero.csv', "r"); $data = array(); while (!feof($file)) { $data[] = fgetcsv($file,null,';'); } fclose($file);
How to write/create a CSV
For write or create a CSV with PHP we will follow a procedure similar to reading but in reverse; instead of obtaining data from the file we will insert it from the array.
To write in a CSV I always follow the same procedure, although you can do another one depending on the problem you want to solve.
I always start preparing the CSV information by using a matrix (array of arrays) in this way, going through each of the rows of the matrix I have the perfect structure to insert rows in the CSV, it is exactly what the function PHP’s fputcsv.
Steps to follow:
- Open the file with its path with the php function fopen.
- Loop through the array or content to be inserted. For arrays I always use foreach.
- Insert a line in the CSV with an Array and the function fputcsv( array )
- Close the open file with fclose.
In the following script you can see how I write in the CSV using an array $array_products as the basis for the rows and columns. You have to take into account that by default the CSV read must use the “,” (comma) character as column separator and double quotes as text container.
$outputBuffer = fopen($ruta_csv, 'w'); foreach($matriz_productos as $n_linea => $linea) { fputcsv($outputBuffer, $linea); } fclose($outputBuffer);
Function passing CSV to Array
function process_csv($file) { $file = fopen($file, "r"); $data = array(); while (!feof($file)) { $data[] = fgetcsv($file,null,';'); } fclose($file); return $data; }
Function passing Array to CSV
function write_csv($matriz_productos, $ruta_csv) { if( !file_exists( $ruta_csv ) ); file_put_contents( $ruta_csv, ''); $outputBuffer = fopen($ruta_csv, 'w'); foreach($matriz_productos as $n_linea => $linea) { fputcsv($outputBuffer, $linea, ';', '"'); } fclose($outputBuffer); }