Remote file access script with fopen() and file_get_contents()

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").

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:

<?php

include('read_file_class.php');

Here is the full script:

<?php
class ReadFile{

	var $targetFile;
	var $error;
	var $errorText;
	var $data;
	var $timeout;

	function ReadFile($targetFile){
		//INITIALIZE
		$this->targetFile=$targetFile;
		$this->error=false;
		$this->timeout=10; //TIMEOUT IN SECONDS
		
		if(!empty($this->targetFile)){
			$this->downloadContents();
		}
		else{
			$this->errorText="Filename not specififed in class constuctor";
			$this->error=true;
		}
	}
	
	function getFileContents(){
		if(!$this->hasError()){
			return $this->data;
		}
		return false;
	}
	
	function hasError(){
		if($this->error){
			return true;
		}
		return false;
	}
	
	function getError(){ 
		return $this->errorText; 
	}

	function downloadContents(){
		$data=NULL;
		//CHECK TO SEE IF WE ARE USING PHP 4.3+
		if(function_exists('file_get_contents')){
			$this->data=$this->openWithFileGetContents();
		} 
		else{
			$this->data=$this->openWithFOpen();
		}
	}
	
	function openWithFileGetContents(){
		$data=NULL;
		//SET TIMEOUT (PHP 4.3+ ONLY)
		ini_set('default_socket_timeout', $this->timeout);  
		//RETRIEVE FILE
		if(!$data=@file_get_contents($this->targetFile)){
			$this->errorText='file_get_contents of ' . $this->targetFile . ' failed.';
			$this->error=true;
		}
		return $data;
	}
	
	function openWithFOpen(){
		$data=NULL; 
		//RETRIEVE FILE
		if($dataFile = @fopen($this->targetFile, "r" )){
			while (!feof($dataFile)) {
				$data.= fgets($dataFile, 4096);
			}
			fclose($dataFile);
		}
		else{
			$this->errorText='fopen of ' . $this->targetFile . ' failed.';
			$this->error=true;
		}
		return $data;
	}
}
?>

Initializing 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.

<?php

include('read_file_class.php');

$myReadAccess=new ReadFile('http://www.yahoo.com');

if(!$data=$myReadAccess->getFileContents()){
	
        //ERROR, DISPLAY ERROR MESSAGE
	echo "Error: " . $myReadAccess->getError();
}
else{
	 
        //SUCCESS, DISPLAY FILE CONTENTS
	echo $data;
}

?>

Script Notes

I decided to default to the file_get_contents() function when available, just incase the performance results were skewed from the setup of my 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.