How to read a file using PHP
TAGS: fopen, fread, read file, textfile


<?php
if (isset($_POST['doit']))  { 
$viewme = 'file.txt'; 
$file = fopen($viewme, 'r'); 
$string = fread( $file, filesize($viewme) ); 

echo "<TEXTAREA ROWS='24' COLS='80'>"; 
echo $string; 
echo "</TEXTAREA>"; 
exit(); 
} 
?>
<HTML><BODY> 
<TABLE> 
<FORM METHOD='POST' ACTION=''> 
<TR> 
<TD>Click here to read file: </TD> 
<TD><INPUT TYPE='submit' NAME='doit' VALUE='Read textfile'></TD> 
</TR></FORM> 
</TABLE> 
</BODY></HTML> 


This example shows how to read a textfile and present it in a <TEXTAREA> </TEXTAREA>
Simply put the above code in a php file, and call it from your webserver. Remember you need to have a file named “file.txt” for this example.