Read a Text File With PHP

Read a Text File With PHP

Read a Text File With PHP, the below Code Snippet show how to read content inside a file via PHP .

 

<?php

// Open the File
$file = fopen("C:/xampp/htdocs/testFile.txt", "r");

$i=0;
// as long as is not end of file continue loop through

while(!feof($file)){
	
	// get the file string by line
	$thisLine = fgets($file);
	// Explode the line when there is  a  ", " in between
	$personData[$i]= explode(",",$thisLine);
	

  
    $i++;
	
}
// close the File
fclose($file);
// Number of Rows

$numRows = sizeof($personData);


// Loop Through the Array and print out
for ($x = 0; $x < $numRows; $x++) {
	
	for ($y = 0; $y <sizeof($personData[$x]); $y++) {
		
		echo $personData[$x][$y].",";
		
		
		
	}
	
	echo "<br>";
  
}


?>


 

Leave a Comment

19 − 14 =