Monday, June 2, 2008

PHP - How to Read and write files, and check file permissions

Reading and Writing Files

PHP comes with a powerful and flexible file manipulation API, which enables
developers to view and modify file attributes, read and list directory contents, alter
file permissions, retrieve file contents into a variety of native data structures, and
search for files based on specific patterns. The following sections discuss reading
and writing files, and retrieving file information.
Reading Data from a File
To begin with, let’s consider the process of opening a file and reading its contents.
Create and run the following PHP script (remember to alter the value of the $file
variable to an actual file on your system that is readable by the web server):



// set file to read
$file = '/home/web/projects.txt';
// open file
$fh = fopen($file, 'r') or die('Could not open file!');
// read file contents
$data = fread($fh, filesize($file)) or die('Could not read file!');
// close file
fclose($fh);
// print file contents
echo $data;
?>


A review of the previous script will reveal the three basic steps to reading data
from a file:
  1. Open the file and assign it a file handle: PHP needs a file handle to read data from a file. This file handle can be created with the fopen() function, which accepts two arguments: the name and path to the file, and a string indicating the mode in which the file is to be opened ('r' for read).
  2. Interact with the file via its handle and extract its contents into a PHP variable. If the fopen() function is successful, it returns a file handle— $fh—which can be used for further interaction with the file. This file handle is used by the fread() function, which reads the file and places its contents into a variable. The second argument to fread() is the number of bytes to be read. You can usually obtain this information through the filesize() function, which returns the size of the file in bytes.
  3. Close the file. Once you’re done with the file, it’s a good idea to close it with fclose(), to avoid using up memory. This last step is not strictly necessary, but it’s a good habit to develop.

An alternative method of reading data from a file is the file() function,
which reads the entire file into an array with a single function call. Each element
of the array then contains one line from the file. To display the contents of the file,
simply iterate over the array in a foreach() loop and print each element (line).


Read or Write?


You can use any one of three different modes with the fopen() function:
'r' (opens a file in read mode), 'w' (opens a file in write mode, destroying
existing file contents) and 'a' (opens a file in append mode, preserving existing
file contents). You can also add the 'b' modifier to force the file to open in
binary mode, and the 't' modifier to control how the line-ending character
is handled on different platforms. Read http://www.php.net/manual/en/
function.fopen.php for more information.



// set file to read
$file = '/home/web/projects.txt';
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line)
{
echo $line;
}
?>

No comments: