file-downloader
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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();
?>
/**
* 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();
?>