diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-12 21:08:22 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-12 21:08:22 +0100 |
commit | 948fcd838d9430fd25da70bb8c3d1b0bf5357e6a (patch) | |
tree | 74ab934e472082fa449233789aa33a1a338a0d9d /php_download.php |
first version
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'php_download.php')
-rw-r--r-- | php_download.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/php_download.php b/php_download.php new file mode 100644 index 0000000..dc46b33 --- /dev/null +++ b/php_download.php @@ -0,0 +1,69 @@ +<?php + +class PHP_DW { + private $base_dw = 'downloads'; + private $base_count = '.download_count'; + + private function __get_full_path($path) { + if (basename($path) !== $path) { + return null; + } + + return $this->base_dw . '/' . $path; + } + + private function __incr_count($filename) { + /* open the file */ + $fh = fopen($this->base_count . '/' . $filename . '.cnt', "r+"); + + if (is_resource($fh) and flock($fh, LOCK_EX)) { + $count = fgets($fh); + + if ($count === false) + $count = 0; + + ftruncate($fh, 1); + rewind($fh); + fwrite($fh, $count + 1); + + flock($fh, LOCK_UN); + + fclose($fh); + } + } + + public function download($filename) { + $path = $this->__get_full_path($filename) or die('invalid file'); + + if (@readfile($path) !== false) { + header('Content-Description: File Transfer'); + header('Content-Disposition: attachment; filename=' . $filename); + header('Content-Length: '. filesize($path)); + header('Cache-Control: must-revalidate'); + + $this->__incr_count($filename); + } + + } + + public function get_count($path) { + if (!$this->__is_valid_path($path)) return 0; + + $fh = @fopen($this->base . $path, "r"); + + if (!is_resource($fh)) + return 0; + + $count = fgets($fh) or '0'; + + fclose($fh); + + return $count; + } +} + +$var = new PHP_DW(); + +$var->download($_GET['file']); + +?> |