Scripts
file Downloader
Naar aanleiding van het script van Koko op 13 maart ( http://www.phphulp.nl/php/scripts/2/948/ ) en de reacties hierop heb ik even kort met wat copy/paste werk een class gemaakt voor het geforceert downloaden.
file-downloader
[code]
<?php
/**
* file class
* @author Ruud Verbij
* @version 1.0
*/
class FileDownloader {
private $fileName;
private $fileSize;
private $fileContent;
/** constructor, needs filename to collect data */
function __construct($fileName) {
$this->fileName = $fileName;
}
/**
* returns content of the file
* @param save says if you want to save the content for easier access
*/
public function getContent($save = false) {
$content = file_get_contents($this->fileName);
$this->fileContent = ($save) ? $content : "";
$this->fileSize = strlen($content);
return $content;
}
/** forces the download to start */
public function forceDownload() {
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-type: application/octetstream");
header("Content-Disposition: attachment; filename=\"".$this->fileName."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$this->fileSize);
set_time_limit(0);
echo $this->getContent();
}
}
/* USAGE */
$file = new FileDownloader("http://www.okiedokie.nl/postcard/pictures/kusje.jpg");
$file->forceDownload();
?>
[/code]
Reacties
0