From ce3ac0671bce24b0af715de77bc1f9b311c4b11f Mon Sep 17 00:00:00 2001 From: Olivier Gayot Date: Mon, 12 Jan 2015 22:20:03 +0100 Subject: added a working version of the class and a simple download script Signed-off-by: Olivier Gayot --- PHP_DW.php | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ download.php | 12 ++++++++++ php_download.php | 69 -------------------------------------------------------- 3 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 PHP_DW.php create mode 100644 download.php delete mode 100644 php_download.php 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 @@ +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; + } +} +?> diff --git a/download.php b/download.php new file mode 100644 index 0000000..6ec810f --- /dev/null +++ b/download.php @@ -0,0 +1,12 @@ +download($_GET['file']); +} + +?> diff --git a/php_download.php b/php_download.php deleted file mode 100644 index dc46b33..0000000 --- a/php_download.php +++ /dev/null @@ -1,69 +0,0 @@ -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']); - -?> -- cgit v1.2.3