Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added send_file() here. SC#23.

Retrofitted from HEAD
  • Loading branch information...
commit bbb34b78a6257f2c351faa28b5d897829dc68df0 1 parent ff9a78a
authored December 16, 2004

Showing 1 changed file with 65 additions and 1 deletion. Show diff stats Hide diff stats

  1. 66  files/mimetypes.php
66  files/mimetypes.php
@@ -79,7 +79,7 @@ function mimeinfo($element, $filename) {
79 79
     );
80 80
 
81 81
     if (eregi("\.([a-z0-9]+)$", $filename, $match)) {
82  
-        if(isset($mimeinfo[strtolower($match[1])][$element])) {
  82
+        if (isset($mimeinfo[strtolower($match[1])][$element])) {
83 83
             return $mimeinfo[strtolower($match[1])][$element];
84 84
         } else {
85 85
             return $mimeinfo["xxx"][$element];   // By default
@@ -89,4 +89,68 @@ function mimeinfo($element, $filename) {
89 89
     }
90 90
 }
91 91
 
  92
+function send_file($path, $filename, $lifetime=86400 , $filter=false, $pathisstring=false) {
  93
+
  94
+    $mimetype     = mimeinfo('type', $filename);
  95
+    $lastmodified = $pathisstring ? time() : filemtime($path);
  96
+    $filesize     = $pathisstring ? strlen($path) : filesize($path);
  97
+
  98
+    @header('Last-Modified: '. gmdate("D, d M Y H:i:s", $lastmodified) .' GMT');
  99
+    if ($lifetime > 0) {
  100
+        @header('Cache-control: max-age='.$lifetime);
  101
+        @header('Expires: '. gmdate("D, d M Y H:i:s", time() + $lifetime) .'GMT');
  102
+        @header('Pragma: ');
  103
+    } else {
  104
+        // this part is tricky, displaying of MS Office documents in IE needs
  105
+        // to store the file on disk, but no-cache may prevent it
  106
+        @header('Cache-Control: private, must-revalidate, pre-check=0, post-check=0, max-age=10');
  107
+        @header('Expires: '. gmdate("D, d M Y H:i:s", 0) .'GMT');
  108
+        @header('Pragma: no-cache');
  109
+    }
  110
+    @header('Accept-Ranges: none'); // PDF compatibility
  111
+    @header('Content-disposition: inline; filename='.$filename);
  112
+
  113
+    if (!$filter) {
  114
+        @header('Content-length: '.$filesize);
  115
+        if ($mimetype == 'text/plain') {
  116
+            @header('Content-type: text/plain; charset='.get_string('thischarset')); //add encoding
  117
+        } else {
  118
+            @header('Content-type: '.$mimetype);
  119
+        }
  120
+        if ($pathisstring) {
  121
+            echo $path;
  122
+        }else {
  123
+            readfile($path);
  124
+        }
  125
+    } else {     // Try to put the file through filters
  126
+        if ($mimetype == 'text/html') {
  127
+            $options->noclean = true;
  128
+            $text = $pathisstring ? $path : implode('', file($path));
  129
+            $output = format_text($text, FORMAT_HTML, $options, $course->id);
  130
+
  131
+            @header('Content-length: '.strlen($output));
  132
+            @header('Content-type: text/html');
  133
+            echo $output;
  134
+        } else if ($mimetype == 'text/plain') {
  135
+            $options->newlines = false;
  136
+            $options->noclean = true;
  137
+            $text = htmlentities($pathisstring ? $path : implode('', file($path)));
  138
+            $output = '<pre>'. format_text($text, FORMAT_MOODLE, $options, $course->id) .'</pre>';
  139
+
  140
+            @header('Content-length: '.strlen($output));
  141
+            @header('Content-type: text/html; charset='. get_string('thischarset')); //add encoding
  142
+            echo $output;
  143
+        } else {    // Just send it out raw
  144
+            @header('Content-length: '.$filesize);
  145
+            @header('Content-type: '.$mimetype);
  146
+            if ($pathisstring) {
  147
+                echo $path;
  148
+            }else {
  149
+                readfile($path);
  150
+            }
  151
+        }
  152
+    }
  153
+    die; //no more chars to output!!!
  154
+}
  155
+
92 156
 ?>

0 notes on commit bbb34b7

Please sign in to comment.
Something went wrong with that request. Please try again.