From: Andrey Kutejko Date: Sat, 5 Oct 2013 20:27:36 +0000 (+0300) Subject: file download respoonse X-Git-Tag: 0.5~21 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=068abe886da47ae91eaf04f6e6387cc041570602;p=ipf.git file download respoonse --- diff --git a/ipf/http/response.php b/ipf/http/response.php index cdb02ce..071b5af 100644 --- a/ipf/http/response.php +++ b/ipf/http/response.php @@ -65,15 +65,17 @@ class IPF_HTTP_Response function render($output_body=true) { - if ($this->status_code >= 200 - && $this->status_code != 204 - && $this->status_code != 304) { + if ($this->status_code >= 200 + && $this->status_code != 204 + && $this->status_code != 304 + && !ArrayTools::get($this->headers, 'Content-Length')) { + $this->headers['Content-Length'] = strlen($this->content); } + $this->outputHeaders(); - if ($output_body) { - echo($this->content); - } + if ($output_body) + $this->outputBody(); } function outputHeaders() @@ -105,4 +107,10 @@ class IPF_HTTP_Response } } } + + function outputBody() + { + echo $this->content; + } } + diff --git a/ipf/http/response/file.php b/ipf/http/response/file.php new file mode 100644 index 0000000..0d5e6c6 --- /dev/null +++ b/ipf/http/response/file.php @@ -0,0 +1,24 @@ +filename = $filename; + $this->headers['Content-Description'] = 'File Transfer'; + $this->headers['Content-Disposition'] = 'attachment; filename='.basename($downloadName); + $this->headers['Content-Transfer-Encoding'] = 'binary'; + $this->headers['Content-Length'] = filesize($filename); + } + + function outputBody() + { + if (ob_get_level()) + ob_end_clean(); + readfile($this->filename); + } +} +