Fri Jun 26 2020

Read CSV file

PHP Scripting923 views

File Name: read-csv.php

<?php
	/* Open file in read mode */
	$file = fopen('csvdata.csv', 'r');
	
	if(!$file)
		die("Can't open csvdata.csv");
	
	/* Declare array variable */
	$data = array();
	
	/* Gets line from file pointer and parse for CSV fields */
	while (!feof($file)) {
		$data[] = fgetcsv($file);
	}
	
	/* Count the number of rows in CSV file */
	$crow = count($data);
	
	/* Print data from array */
	for($i=1; $i < $crow-1; $i++ ) {
		echo "Name: ".$data[$i][0]."<br />";
		echo "Contact: ".$data[$i][1]."<br />";
		echo "E-mail: ".$data[$i][2]."<br /><br />";
	}
?>



/* Output */
Name: Jonh
Contact: +1 111 222 4444
E-mail: jonh@gmail.com

Name: Sunny
Contact: +2 555 444 8888
E-mail: sunny@gmail.com

Name: James
Contact: +5 777 999 3131
E-mail: james@gmail.com
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.