Changes

PHP, Filesystems and File I/O

405 bytes added, 19:58, 11 May 2009
Reading From a File using PHP
<tt>data = This line of text was written by PHP</tt><br>
 
Note the use of the "OR die()" syntax in the above example. The reason for this is to call the ''die()'' function if the specified file cannot be opened. The purpose of the built-in PHP ''die()'' function is to display a message (specified within the parentheses) and then exit the script. This prevents the remaining PHP code from executing and attempting to read from a file which could not be opened.
It is also possible to read and output the contents of an entire file with the ''readfile()'' function. This function reads the entire contents of a file and outputs that content. Assuming you don't need to do anything but output the contents of a file then ''readfile()'' is an easy solution because it does all the work for you. You do not need to open the file, read the data, close the file and display the data. All you need to do is call ''readfile()'' passing in the file path as an argument and it does the rest.