Read File

<?php
	$fname = "myFile.txt";
	
	/* Get file size */
	$fsize = filesize($fname);
	
	/* Open file in read mode */
	$file = fopen($fname, "r" );
	
	/* Read file using 'fread' */
	$data = fread($file, $fsize );	
	echo $data."<br />";
	
	/* Close file */
	fclose($file); 
	

	/* Open file in read mode */
	$file2 = fopen($fname, "r");

	/* Parses input from a file according to a format using 'fscanf' */
   while ($info = fscanf($file2, "%s %s %s\n")) {
   	
   	/* List according to reading format  */
      list($p1,$p2,$p3) = $info;
      echo $p1." ".$p2." ".$p3."<br />";
   }
   
   /* Close file */
   fclose($file2);
?>


/* Ouput */
My first file! This is a text file!

My first file!

This is a

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!