]> git.andy128k.dev Git - ipf.git/commitdiff
file download respoonse
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 5 Oct 2013 20:27:36 +0000 (23:27 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 5 Oct 2013 20:27:36 +0000 (23:27 +0300)
ipf/http/response.php
ipf/http/response/file.php [new file with mode: 0644]

index cdb02ce52827bd8ef22de0d1cf53bf1ed1b6366d..071b5af103c2af985988ca3af9b17dc833d221b5 100644 (file)
@@ -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 (file)
index 0000000..0d5e6c6
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+
+class IPF_HTTP_Response_File extends IPF_HTTP_Response
+{
+    private $filename;
+
+    function __construct($filename, $downloadName, $mimetype='application/octet-stream')
+    {
+        parent::__construct('', $mimetype);
+        $this->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);
+    }
+}
+