diff options
author | Olivier Gayot <duskcoder@gmail.com> | 2015-01-13 22:06:19 +0100 |
---|---|---|
committer | Olivier Gayot <duskcoder@gmail.com> | 2015-01-13 22:06:19 +0100 |
commit | 3abef20b2a0ecaeff1877eb3d10bb12338041c49 (patch) | |
tree | 26f80aced76cf07920c7ace3eae49405eb0819fc /PHP_DW.php | |
parent | 3bdf0b110f4c5880307e871fbcdb737ee6f915da (diff) |
rewritten the method `download'
the method is not vulnerable to race conditions anymore.
BTW, the headers are set before the file is actually written.
Signed-off-by: Olivier Gayot <duskcoder@gmail.com>
Diffstat (limited to 'PHP_DW.php')
-rw-r--r-- | PHP_DW.php | 32 |
1 files changed, 24 insertions, 8 deletions
@@ -46,18 +46,34 @@ class PHP_DW { } 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'); - $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'); + $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); - $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 { - die('no such file or directory'); + echo($path . ': no such file or directory'); } } |