File Handling - LEARN PHP IN A DAY: The Ultimate Crash Course to Learning the Basics of PHP in No Time (2015)

LEARN PHP IN A DAY: The Ultimate Crash Course to Learning the Basics of PHP in No Time (2015)

Chapter 9. File Handling

Introduction

File handling is an important part of all web applications. You will sometimes find it necessary to open and process a file or files for different reasons and tasks. With that being said, we must issue a word of caution. When you are manipulating files you must be very careful. You can do a lot of damage if you do something wrong. Common errors are: editing the wrong file, filling a hard-drive with garbage data, and deleting the content of a file by accident.

File Handling

Reading Files

The readfile() function reads a file and writes it to the output buffer.

Assume we have a text file called "webdictionary.txt", stored on the server, that looks like this:

The PHP code to read the file and write it to the output buffer is as follows (the readfile() function returns the number of bytes read on success):

Opening files

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

We will use the text file, "webdictionary.txt", during the lessons:

The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

Modes

Description

r

Open a file for read only. File pointer starts at the beginning of the file

w

Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file

a

Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist

x

Creates a new file for write only. Returns FALSE and an error if file already exists

r+

Open a file for read/write. File pointer starts at the beginning of the file

w+

Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file

a+

Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist

x+

Creates a new file for read/write. Returns FALSE and an error if file already exists

PHP Reading files

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the "webdictionary.txt" file to the end:

PHP Closing files

The fclose() function is used to close an open file.

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:

Tip!

It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!

PHP Create File

The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files.

If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).

The example below creates a new file called "testfile.txt". The file will be created in the same directory where the PHP code resides:

PHP Write to File

The fwrite() function is used to write to a file.

The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

The example below writes a couple of names into a new file called "newfile.txt":

Notice that we wrote to the file "newfile.txt" twice. Each time we wrote to the file we sent the string $txt that first contained "John Doe" and second contained "Jane Doe". After we finished writing, we closed the file using the fclose() function.

If we open the "newfile.txt" file it would look like this:

PHP Overwriting

Now that "newfile.txt" contains some data we can show what happens when we open an existing file for writing. All the existing data will be ERASED and we start with an empty file.

In the example below we open our existing file "newfile.txt", and write some new data into it:

If we now open the "newfile.txt" file, both John and Jane have vanished, and only the data we just wrote is present:

Conclusion

With that we conclude our discussion of PHP File Handling. You would find file handling to be useful if you want to store information on your server, but you don’t need a construct like a database because you are storing a small amount of information that will not change frequently, for example. There are many other examples for when File Handling would be useful, but we shall not go into analyzing those.