How to Write Text to Files in PHP for HTML5 and CSS3 Programming

By Andy Harris

Here, you will find details about the functions needed to access and write to a file in PHP, such as how to request access to a file from PHP with the fopen() function, write to the file using the fwrite() function, and let PHP know you are done with the file with the fclose() function.

fopen()

To do any file manipulations, you must tell PHP about the file you would like to manipulate and tell PHP how you would like to manipulate that file.

The fopen() function has two required parameters that you must pass to it: the path to the file and the type of file manipulation you would like to perform (the mode).

The fopen() function returns a connection to the requested file if it’s successful. (The connection is called a pointer). If there is an error, the function returns False. Whatever the fopen() function returns (the connection or False), it should be assigned to a variable (a stream).

Here is an example of the function:

$fileConnection = fopen($theFile, $theMode);

In the preceding example, the file connection returned by the fopen() function is assigned to the variable $fileConnection. The variable $theFile would contain the path to a file. The file must be in a place the server can access, meaning that you can put the file anywhere you could put a PHP page for the server to serve.

Although possible, you probably shouldn’t try to connect to a file in the my Documents folder or its equivalent on your operating system. You’ll need the actual file path, which can be quite convoluted. It’s also not necessary for the files you open to be in the htdocs directory. This could be useful if you want to access a file that will not be available except through your program.

Use a relative reference if the file will be in the same directory as your program, or use an absolute reference if it will be somewhere else on your system. If you move your program to a remote server, you can only access files that reside on that server.

The variable $theMode would contain one of the values from the following list:

  • r: Grants read-only access to the file

  • w: Grants write access to the file

  • Be careful, though, because if you specify this mode (w) for the fopen() function and use the fwrite() function, you will completely overwrite anything that may have been in the file. Don’t use if there’s anything in the file you want to keep.

  • a: Grants the right to append text to the file. When you specify this mode for the fopen() function and use the fwrite()function, the fwrite()function appends whatever text you specify to the end of the existing file.

  • r+ or w+: Grants read and write access to the file. They’re a special way of accessing the file, called random access. This allows you to simultaneously read and write to the file. If you require this type of access, you probably should be using something more simple and powerful, like relational databases.

fwrite()

After you open a file with the fopen()function and assign the file connection to a variable, you can use the file in your PHP code. You can either read from the file, or you can write to the file with the fwrite()function.

Depending on what mode you specify when you opened the file with the fopen()function, the fwrite()function either overwrites the entire contents of the file (if you used the w mode) or it appends the text you specify to the end of the file (if you used the a mode).

The fwrite()function has two required parameters you must pass to it: the connection to the file that was established by the fopen()function and the text you wish to write to the file. The fwrite()function returns the number of bytes written to the file on success and False on failure.

Here is an example of the fwrite()function:

$writeResults = fwrite($fileConnection, $text);

The fwrite()function can also be written fputs().fwrite() and fputs().

fclose()

After you finish working with the file, closing the file connection is important.

To close the connection to a file you’ve been working with, you must pass the connection to the file you wish to close to the fclose() function. The fclose()function will return True if it is successful in closing the connection to the file and False if it is not successful in closing the connection to the file.

Here is an example of the function:

fclose($fileConnection);