Placing a File s Contents into a Variable

Oct 07, 2009 Author: City Hall

Let's say that you want to put all of the content of a text file into a variable so that you can access it later. This is a good introduction to file access, because it shows all of the basic steps. Here's how you would place the contents of file.txt into the $file_data variable:

<?php

$file_data = '';

$fd = fopen('file.txt', 'r');
if (!$fd) {
    echo "Error! Couldn't open the file.";
    die;
}

while (! feof($fd)) {
    $file_data .= fgets($fd, 5000);

}

fclose($fd);

?>
The fopen() function is an essential first step in most file access. It acts as a gateway between the system and PHP. When opening a file, you specify how you want to access the file. In this case, we open it for read access. You can also open a file for write access. fopen() returns a resource ID that you use with other functions to perform file operations. In this case, we use fgets() and feof() to do the actual work. fopen() has two parameters: the file's path and the mode. Here are the commonly useful modes (keep in mind that any of these can fail if you don't have the proper permissions for it). r Open for reading only; start from the beginning of the file. w Open for writing only (see the following section); start from the beginning of the file, and erase the contents of the file. If the file does not exist, attempt to create it. x Create and open for writing only; start from the beginning of the file. If the file already exists, the fopen() call returns false. This option is supported in PHP 4.3.2 and later. a- Open for writing only start at the end of the file. If the file does not exist, attempt to create it. The following file operations open the file for both reading and writing. Don't use them unless you really know what you're doing: w+ Open for reading and writing; start from the beginning of the file, and erase the contents of the file if it has any. If the file does not exist, attempt to create it. r+ Open for reading and writing; start from the beginning of the file. a+ Open for reading and writing; start at the end of the file. If the file does not exist, attempt to create it. x+ Create and open for reading and writing; start from the beginning of the file. If the file already exists, the fopen() function call returns false. This option is supported in PHP 4.3.2 and later. Getting back to the script, $fd = fopen('file.txt', 'r') means Open this file to read only and assign the resource ID to $fd. To see if the open worked correctly, we just see if $fd has a value. Now we're ready to do the real work in a loop. The feof() function tells us when we've hit the end of the file, so we use that as a test condition to break the loop. The fgets() function call here retrieves the next line of the file, up to 5,000 bytes' worth at a time. We append that to $file_data. When finished, we use fclose($fd) to free system resources and tell the system that we're finished with file access.


views 2985
  1. Add New Comment