PHP includes several built-in functions that make it possible to open, read, write, and append to files.
Whether you are creating log files or digesting a list of products from a CSV file, these powerful file manipulation functions are well worth having in your PHP toolbox, and fopen() is the function that starts them all.
As its name implies, fopen binds a file resource to a stream — effectively opening it.
This function requires two arguments, a filename and a mode.
The filename is simple enough. But you will need to be careful when you specify the path, since there can be differences between operating systems. It is worth noting that fopen can read several file types, including text files, comma separated files, HTML documents, PDFs, or even compressed files.
The mode parameter tells fopen what kind of stream access you require. Here are the mode definitions according to the PHP manual.
• r – Opens a file for reading only and places the file pointer at the beginning of the file. • r+ – Opens that file for both reading and writing and places the file pointer at the beginning of the file. • w – Opens the file for writing only and will create the file if it does not exist. This will overwrite an existing file. • w+ – Overwrites the file and opens it for reading and writing. • a – Opens a file for reading and writing, placing the file pointer at the beginning of the file. If the file does not exist, this mode will seek to create it. • a+ – Opens the file for reading and writing, placing the file pointer at the end of the file. Again, if the file does not exist, this mode will seek to create it. • x – Creates and opens a file for writing only. If the file already exists, this mode will generate an error. • x+ – Creates and opens a file for reading or writing. It will also generate an error if the file already exists. • c – Opens the file for writing only, attempting to create any file that does not exist. • c+ Just like c, but opens the file for both writing and reading.
Often, fopen is used as the assignment value for a variable representing the file’s “handle,” and, frequently, this “handle” variable is actually called $handle for clarity.
In the example above, PHP would open the file somefile.csv for read only.
Now, if the file did not already exist, PHP would try to create it. If the file did exist, PHP would overwrite it.
If you are following the pattern, you know that the code above would open the file for reading and writing.
Above, fopen will retrieve the HTML document for a recent showcase article published here on Ecommerce Developer.
You can test if your fopen is working with the help of fgets(), which retrieves a line from the open file stream. The second parameter, fgets(), represents the number of characters desired.
As a best practice, you should employ fclose to, well, close and files you’ve opened with fopen.
In PHP, the built in function fopen allows you to open all sorts of files and gain access to manipulate those files’ contents. This is especially useful, for example, if you want to transfer products from one ecommerce platform or shopping cart to another, since you can have one cart output the products in a CSV file and the second platform ingest them with fopen.