Loading Files

Opening and loading files are very common programming operations, and fortunately they are not difficult.

Opening a File

Opening a file requires the use of the fopen() function, which gives you access to any file and its contents. Let’s look at its declaration:

fopen($filename, $mode)

Both parameters are strings. Obviously $filename is intuitive; however, $mode is a little bit more complex. Let’s take a look and let me explain what that parameter means.

When you open a file, you can open it for several purposes—the most common are reading and writing. You can also open a file for appending, which means that you can add information only to the end of a file. Table 14.2 shows what the PHP manual has to say about all the possible modes.

Table 14.2. fopen() Modes
ModeDescription
rThis opens the file for reading only. The file pointer is put at the beginning of the file.
r+This opens the file for reading and writing. The file pointer is put at the beginning of the file.
wThis opens the file for writing only. The file pointer is put at the beginning of the file. If the file does not exist, this attempts to create it.
w+This opens the file for reading and writing. The file pointer is put at the beginning of the file. If the file does not exist, this attempts to create it.
aThis opens the file for writing only. The file pointer is put at the end of the file. If the file does not exist, this attempts to create it.
a+This opens the file for reading and writing. The file pointer is put at the end of the file. If the file does not exist, this attempts to create it.

There aren’t too many modes. Yeah, there are two more modes, x and x+, which look weird, but because they aren’t used much, I won’t spend any time on them in this chapter.

Only use the most limited of modes depending on your situation. If you need to use a file because you need to read data from it, use the r mode. If you need to create and write a file, use w. Even though you can use w+ to read or write to the file, it is safer to use the mode that allows only what you need. Otherwise, you can accidentally overwrite a file.

The fopen() function returns a handle to the file. A handle is essentially the same as a variable, and it stores all the data inside the file. So, if you wanted to open a file for reading, you could do it like this:

$file = fopen('file.txt', 'r'),

All later operations are done upon the $file handle variable.

Closing a File

When you are done with a file inside your PHP program, close it. If you leave it open, the script can take up more space and slow down the system. Use the fclose() function to close the file. The declaration for this function is as follows:

fclose($handle);

Simply pass the variable that you retrieved from fopen() to fclose() to close the file, and the file disappears! Loading and closing a file could be done as follows:

<?
$handle = fopen('file.txt','w'),
//Do file operations, all writing because of mode

fclose($handle);
?>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.250.1