A file proxy class

In a recent web project of mine, it was needed to offload some mp3 files to another server as per the hosting providers specifications. 😉 these could not be overidden since the service was free for a specific purpose. The database was on a different server, and as most of you know, this does not affect php a bit.

But the media bifurcation did take me for a spell. On my test bed, I was using readfile() to read the contents of the mp3file to the browser, after providing correct header tags. In the test server this was working fine, since the file urls were relative ofcourse. I checked through the hosting system using phpinfo() and did confirm the url_fopen wrappers were enabled. But to my dismay, when loaded on to the hosting space, it seemed that the readfile was failing and hence I needed a different method.

Then like a thunderbolt this idea of a file proxy class came to my mind. And this was implemented. It works for me and my project. There may be different view points, as well as enhancements. I would appreciate it if some one could enhance it in case the url_fopen wrappers is disabled in the php configuration.


class fileProxy
{

var $status;
var
$url;
function
fileProxy($url){
$this->status = array(‘success’ => true);
$parsed = parse_url($url);
if(!isset(
$parsed[‘scheme’]))
$this->status = array(‘success’ => false, ‘error’ => ‘Invalid URL’);
if(!isset(
$parsed[‘host’]))
$this->status = array(‘success’ => false, ‘error’ => ‘Invalid host in url’);
if(!isset(
$parsed[‘path’]))
$this->status = array(‘success’ => false, ‘error’ => ‘Invalid path in url’);
if(
ini_get(‘allow_url_fopen’) == 0)
$this->status = array(‘success’ => false, ‘error’ => ‘URL-aware fopen wrappers is disabled’);

if(!$this->status[‘success’]) return;
$this->url = $url;
}

function readFromProxy(){
$handle = fopen($this->url, “rb”);
while (!
feof($handle))
echo
fread($handle, 8192);
fclose($handle);
}

}