Ok, you know the basics. Ask questions if you don’t understand (by posting comments - it’s the only way to make this tutorial complete!).
To read a text file use the following PHP code:
<?php
$filename = 'test.txt';
$fp = fopen($filename, "r");
$data = fread($fp, filesize($filename));
fclose($fp);
?>
Ok so we read the contents of the file ‘test.txt’, by opening the file for READ ONLY using the letter “r”…
fopen($filename, "r");
Then we inserted the contents of the file ‘test.txt’ into a variable named $data.
$data = fread($fp, filesize($filename));
Once we had finished, we closed the file (to avoid ANY file corruption, errors etc…) by simply using:
fclose($fp);
If you want to output the contents of the file just add the following line under the ‘fclose($fp);’ bit:
echo $data;
Because we read the file contents into the variable $data using ‘fread()’. As you should know, using ‘echo’ will output the data to the webpage (-;
Just wanna point out, if you want to concatenate text/numbers to a variable, you can use the concatenate function like this:
echo "I am SO 1337<br /><br />".$data;
A simple full-stop (or ‘period as americans call it) means ‘ADD THE FOLLOWING ONTO THE LINE’ on in proper english ‘CONCATENATE’ the two strings. Yes you can do more like this:
echo "I am SO 1337<br /><br />".$data."<br /><br /> Ok, I'm done reading the data in the file.";
