Read from a File
How do you read from a file using PHP?
.. this could be any filetypes that are flatfiles. Like PHP, CSS, HTML, XML, TXT, Javascripts, and many many more.
fopen have following options;
'r' (read only, file pointer at the beginning)
'r+' (read and write, file pointer at the beginning)
'w' (write only, file pointer at the beginning, zero length file, create it if it does not exist)
'w+' (read and write, file pointer at the beginning, zero length file,
create it if it does not exist)
'a' (write only, file pointer at the end, zero length file)
'a+' (read and write, file pointer at the end, zero length file)
'x' (write only, file pointer at the beginning, if exists, return FALSE)
'x+' (read and write, file pointer at the beginning, if exists, return FALSE)
HTML
<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>
PHP
<?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();
}
?>