diff options
Diffstat (limited to 'PHP_DW.php')
-rw-r--r-- | PHP_DW.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/PHP_DW.php b/PHP_DW.php new file mode 100644 index 0000000..ee6a835 --- /dev/null +++ b/PHP_DW.php @@ -0,0 +1,69 @@ +<?php + +class PHP_DW { + private $base_dw = 'downloads'; + private $base_count = '.count'; + + private function __get_full_path($path) { + if (basename($path) !== $path) { + return null; + } + + return $this->base_dw . '/' . $path; + } + + private function __incr_count($filename) { + $fh = fopen($this->base_count . '/' . $filename . '.cnt', "a+"); + + if (is_resource($fh) and flock($fh, LOCK_EX)) { + rewind($fh); + + $count = fgets($fh); + + if ($count === false) + $count = 0; + + $count++; + + ftruncate($fh, 0); + fwrite($fh, $count); + + flock($fh, LOCK_UN); + + fclose($fh); + } + } + + public function download($filename) { + $path = $this->__get_full_path($filename) or die('invalid file'); + + $ret = @readfile($path); + if ($ret) { + 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); + } else { + die('no such file or directory'); + } + } + + public function get_count($filename) { + if (!$this->__get_full_path($filename)) + return 0; + + $fh = @fopen($this->base_count . '/' . $filename . '.cnt', "r"); + + if (!is_resource($fh)) + return 0; + + $count = fgets($fh) or '0'; + + fclose($fh); + + return $count; + } +} +?> |