PHP & Web Development :: Remote file access script with fopen() and file_get_contents() - 7/1/2007

Bullet The Basics

This is an example object-oriented PHP class for remote file access that works with older versions of PHP and has simple error management. You can open a remote website (ex. "http://www.google.com"), an xml feed (ex. "http://rss.news.yahoo.com/rss/us"), or even a local file (ex. "files/textdata.txt").

Bullet The Class

This script is object oriented and written for both older and newer versions of PHP, which differ in their handling of objects. Being object oriented allows multiple instances of this file access object to exist, allowing many files to be opened at once. While you can copy this class into each page that you want to use it on, the best way is to copy it into its own PHP file and include it at the top of each page you want to use it in. For example:

Here is the full script:

BulletInitializing the Class Object

In your PHP file, you need to initialize an object instance of the ReadFile class. To do this, call "$myReadAccess=new ReadFile($url);" as show in the below snippet that uses yahoo as the target URL. Now you have a properly initialized $myReadAccess instance on the ReadFile class that has hopefully downloaded the contents of Yahoo.com to a string.

To test if it completed successfully, call the getFileContents() function on your object instance. For example, in the below script, we call the getFileContents() on our $myReadAccess object and assign the results to the $data variable. If $data is equal to false, then we had an error. If not, then $data contains the contents of the url.

If $data = false, then we can call the getError function on our $myReadAccess object to return a detailed error message.

BulletScript Notes

Some other points of interest in this script is the check to see if the file_get_contents() function exists. If it does, then we are using PHP 4.3+. If not, we have to use another function to open the file. In this case, I used fopen() as the backup file open function. Supposedly, file_get_contents() is faster and the preferred method according to the official PHP documentation, but my own file_get_contents() and fopen() benchmarking experiment shows fopen is actually slightly faster on my server. I decided against the results of my own experiment to default to the file_get_contents() function when available, just incase the performance results were skewed from the setup of my GoDaddy.com hosted server. You can modify the above code easily to use just fopen() instead if desired.

You will also notice that there is a $timeout variable in the class. This can be set to any integer, in seconds, as the maximum time that file_get_contents() will try to open the file. We do not use this for fopen(), because in our script we use fopen() for older versions of PHP, but the "default_socket_timeout" directive which we set to the timeout variable is only available in PHP 4.3+.

The error management here is a simple implementation, but it could be expanded greatly if needed.