dw_dir = $dw_dir; } if ($cnt_dir) { $this->cnt_dir = $cnt_dir; } } private function __get_full_path($path) { if (basename($path) !== $path) { return null; } return $this->dw_dir . '/' . $path; } private function __incr_count($filename) { $fh = @fopen($this->cnt_dir . '/' . $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) { /* according to stat(2) */ define('S_IFMT', 0170000); define('S_IFREG', 0100000); $path = $this->__get_full_path($filename) or die('invalid file'); $fh = @fopen($path, 'rb'); if ($fh) { $fstats = fstat($fh); /* check if the target is a regular file */ if (S_IFREG == ($fstats['mode'] & S_IFMT)) { $this->__incr_count($filename); header('Content-Description: File Transfer'); header('Content-Disposition: attachment; filename=' . $filename); header('Content-Length: '. $fstats['size']); header('Cache-Control: private'); echo(fread($fh, $fstats['size'])); } else { echo($path . ': is not a regular file'); } fclose($fh); } else { echo($path . ': no such file or directory'); } } public function get_count($filename) { if (!$this->__get_full_path($filename)) return 0; $fh = @fopen($this->cnt_dir . '/' . $filename . '.cnt', "r"); if (!is_resource($fh)) return 0; $count = fgets($fh) or '0'; fclose($fh); return $count; } public function get_count_dir() { return $this->cnt_dir; } public function get_download_dir() { return $this->dw_dir; } } ?>