Outputs file contents directly to browser, hiding the actual path of the file.
More...
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->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->removeExtension( 'zip' );
$dl->addExtension( 'zip', 'application/zip' );
You can restrict download to users by using the allowedReferrer
method :
$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->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.
try{
$dl->output();
echo $this->debug->exception($e);
}