Read CSV file

<?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: [email protected]

Name: Sunny
Contact: +2 555 444 8888
E-mail: [email protected]

Name: James
Contact: +5 777 999 3131
E-mail: [email protected]

Comments (0)

  • To add your comment please or

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

Got It!