diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-12 22:20:03 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-12 22:20:03 +0100 |
commit | ce3ac0671bce24b0af715de77bc1f9b311c4b11f (patch) | |
tree | 8e2c8e0281fb2295c29be07fe3dd48e7789dc9c7 | |
parent | 948fcd838d9430fd25da70bb8c3d1b0bf5357e6a (diff) |
added a working version of the class and a simple download script
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
-rw-r--r-- | PHP_DW.php (renamed from php_download.php) | 32 | ||||
-rw-r--r-- | download.php | 12 |
2 files changed, 28 insertions, 16 deletions
diff --git a/php_download.php b/PHP_DW.php index dc46b33..ee6a835 100644 --- a/php_download.php +++ b/PHP_DW.php @@ -2,7 +2,7 @@ class PHP_DW { private $base_dw = 'downloads'; - private $base_count = '.download_count'; + private $base_count = '.count'; private function __get_full_path($path) { if (basename($path) !== $path) { @@ -13,18 +13,20 @@ class PHP_DW { } private function __incr_count($filename) { - /* open the file */ - $fh = fopen($this->base_count . '/' . $filename . '.cnt', "r+"); + $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; - ftruncate($fh, 1); - rewind($fh); - fwrite($fh, $count + 1); + $count++; + + ftruncate($fh, 0); + fwrite($fh, $count); flock($fh, LOCK_UN); @@ -35,21 +37,24 @@ class PHP_DW { public function download($filename) { $path = $this->__get_full_path($filename) or die('invalid file'); - if (@readfile($path) !== false) { + $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($path) { - if (!$this->__is_valid_path($path)) return 0; + public function get_count($filename) { + if (!$this->__get_full_path($filename)) + return 0; - $fh = @fopen($this->base . $path, "r"); + $fh = @fopen($this->base_count . '/' . $filename . '.cnt', "r"); if (!is_resource($fh)) return 0; @@ -61,9 +66,4 @@ class PHP_DW { return $count; } } - -$var = new PHP_DW(); - -$var->download($_GET['file']); - ?> diff --git a/download.php b/download.php new file mode 100644 index 0000000..6ec810f --- /dev/null +++ b/download.php @@ -0,0 +1,12 @@ +<?php + +if (isset($_GET['file']) && $_GET['file']) { + + require_once('PHP_DW.php'); + + $dw = new PHP_DW(); + + $dw->download($_GET['file']); +} + +?> |