Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

The file function returns an entire file as an array. Each line of the file is a separate
element of the array, starting at zero. If it would be more convenient to work with the file
as one string, use the implode function, as I have in the following example. If you are
planning on sending a file directly to the browser, use readfile instead.


<?
// open file
$myFile = file("data.txt");


//fold array elements into one string
$myFile = implode($myFile, "");


//print entire file
print($myFile);
?>


boolean file_exists(string filename)


The file_exists function returns TRUE if the specified file exists and FALSE if it does
not. This function is a nice way to avoid errors with the other file functions. The example
below tests that a file exists before trying to send it to the browser.


<?
$filename = "data.txt";


//if the file exists, print it
if(file_exists($filename))
{
readfile($filename);
}
else
{
print("'$filename' does not exist");
}
?>


integer fileatime(string filename)


The fileatime function returns the last access time for a file in standard timestamp
format, the number of seconds since January 1, 1970. False is returned if there is an error.
A file is considered accessed if it is created, written, or read. Unlike some other file-
related functions, fileatime operates identically on Windows and UNIX.


Two other functions for getting timestamps associated with files are filectime and
filemtime.

Free download pdf