Use PHP to Open Documents in Text, XML, CSV, PDF, and HTML

Posted on Posted in Technology news

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.

fopen()

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.

fopen Modes

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.

fopen Examples

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.

1 <?php
2 $handle = fopen('/somepath/somefile.csv', 'r')

In the example above, PHP would open the file somefile.csv for read only.

1 <?php
2 $handle = fopen('/somepath/somefile.csv', 'w')

Now, if the file did not already exist, PHP would try to create it. If the file did exist, PHP would overwrite it.

1 <?php
2 $handle = fopen('/somepath/somefile.csv', 'a+')

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.

1 <?php
2
4 while(!feof($handle)){
5 echo fgets($handle, 1024) . "<br/>";
6 }
7 fclose($handle);

As a best practice, you should employ fclose to, well, close and files you’ve opened with fopen.

Summing Up

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.

Srinivas Katam

Do you need help growing your business? Srinivas Katam is a Digital Marketing Strategist and Consultant with over 200 successful SEO and Social Media client engagements completed. I’ve worked with brands across the world as a digital marketing consultant to deliver revenue growth from content-focused inbound marketing campaigns. He has been partnering with companies like yours for more than 13+ years to provide more traffic with better results. He is an Innovative Marketer who specializes in digital marketing, specifically in social media, SEO and online strategy.

More Posts - Website

Follow Me:
TwitterFacebookLinkedInPinterestGoogle PlusDiggStumbleUponYouTubeRedditDelicious

Leave a Reply

Your email address will not be published. Required fields are marked *