Read CSV file

PHP Programming

PHP Programming Examples

<?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]

Learn more about fgetcsv and fopen