Public Member Functions | Data Fields
FileDownload Class Reference

Outputs file contents directly to browser, hiding the actual path of the file. More...

Public Member Functions

 FileDownload ($file, $file_name=null)
 
 addExtension ($ext, $value)
 
 removeExtension ($ext)
 
 addAllowedReferrer ($referrer)
 
 setFileName ($file_name)
 
 changeFile ($file)
 
 output ()
 

Data Fields

 $allowed_referrers = array()
 
 $file_name = null
 
 $file = null
 
 $extensions
 

Detailed Description

Outputs file contents directly to browser, hiding the actual path of the file.

Version
1.2
Author
DinuSV

The use of this class comes into play when one wants to hide the actual path to the file or restrict access to it by unknown sources.

The constructor takes 2 argument : the path to the file, and the file name to display when downloading. If the second argument is not given, the name displaed will be hte same as the original name. The output() function will display the file to the browser for download.

$dl = new FileDownload( ROOT . '/myarchive.zip', 'My Collection.zip' );
$dl->output();

You can also set the name of the file after the object was created, using setFileName() :

$dl = new FileDownload( ROOT . '/myarchive.zip' );
$dl->setFileName( 'My Collection.zip' )->output();

By default, the mime type is deducted from the file extension ( The mime type is the value the browser needs in order to know what to do with the file ). The following extensions are known : zip, pdf, doc, xls, ppt, exe, gif, png, jpg, jpeg, mp3, wav, mpeg, mpg, mpe, mov, avi. In order to add more extensions and their mime type, you can use the addExtension() function, and in order to remove them, you can use the removeExtension() function :

$dl = new FileDownload( 'myarchive.zip' );
$dl->removeExtension( 'zip' );
$dl->addExtension( 'zip', 'application/zip' );

You can restrict download to users by using the allowedReferrer method :

$dl = new FileDownload( 'myarchive.zip' );
$dl->addAllowedReferrer( 'http://mydomain.com/download-section' );

To display a message for an unallowed referrer, you need to catch the NoPermissionsException from the output() method.

try{
$dl = new FileDownload( 'myarchive.zip' );
$dl->addAllowedReferrer( 'http://mydomain.com/download-section' );
$dl->output();
} catch ( NoPermissionsException $e ){
echo 'You are not allowed to download this file';
}

Another exception to look for is the InvalidArgumentTypeException in the output() method, which is thrown when the file-type doesn't match the available extensions.

$dl = new FileDownload( 'myarchive.abc' );
try{
$dl->output();
echo $this->debug->exception($e);
}

Member Function Documentation

FileDownload (   $file,
  $file_name = null 
)

Constructor

Parameters
string$file: the full path to the file
string$file_name: [optional] the name of the file to be displayed when downloading
addExtension (   $ext,
  $value 
)

Add an allowed extension and it's mime type

Parameters
string$ext: extension name
string$value: extension mime type
Returns
FileDownload : current object
removeExtension (   $ext)

Remove an allowed extension from the list.

Note
To remove all extensions, the extensions field is public, so just reset it : $this->extensions = array()
Parameters
string$ext: the extension name to be removed
Returns
FileDownload : current object
addAllowedReferrer (   $referrer)

Add an allowed referrer to download the file

Note
If none is set, all referrers will be able to download the file
Parameters
string$referrer: the allowed referrer
Returns
FileDownload : current object
setFileName (   $file_name)

Set the name of the file to display for the download

Parameters
string$file_name: the file name to set
Returns
FileDownload : current object
changeFile (   $file)

Change the file to be downloaded

Parameters
string$file: the full path to the file
Exceptions
FileNotFoundException
Returns
FileDownload : current object
output ( )

Set the headers and output the selected file

Exceptions
NoPermissionsException
InvalidArgumentTypeException

Field Documentation

$allowed_referrers = array()

array : Allowed referrers to download this file

$file_name = null

string : The name of the file to be displayed when downloading

$file = null

string : Full path to the file to be downloaded

$extensions

array : Allowed extensions and their mime type