Get Website Learn Coding Tech Story

PHP Programming Examples

<?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);
?>


          /****** Output ******/
          

My first file! This is a text file!


My first file!


This is a

Learn more about fread and fscanf