866942bae9ec7c6e6fc2d6e9c87b4db952dacb0f
[moodle.git] / file.php
1 <?PHP // $Id$
2       // This function fetches files from the data directory
3       // Syntax:   file.php/courseid/dir/.../dir/filename.ext
5     require("config.php");
6     require("files/mimetypes.php");
8     $lifetime = 86400;
10     if (isset($file)) {     // workaround for situations where / syntax doesn't work
11         $PATH_INFO = $file;
12     }
14     if (!$PATH_INFO) {
15         error("This script DEPENDS on PATH_INFO being available.  Read the README.");
16     }
18     $args = get_slash_arguments();
19     $numargs = count($args);
20     $courseid = (integer)$args[0];
22     $course = get_record("course", "id", $courseid);
24     if ($course->category) {
25         require_login($courseid);
26     }
28     // it's OK to get here if no course was specified
30     $pathname = "$CFG->dataroot$PATH_INFO";
31     $filename = $args[$numargs-1];
33     $mimetype = mimeinfo("type", $filename);
35     if (file_exists($pathname)) {
36         $lastmodified = filemtime($pathname);
38         header("Last-Modified: " . gmdate("D, d M Y H:i:s", $lastmodified) . " GMT");
39         header("Expires: " . gmdate("D, d M Y H:i:s", time() + $lifetime) . " GMT");
40         header("Cache-control: max_age = $lifetime"); // a day
41         header("Pragma: ");
42         header("Content-Length: ".filesize($pathname));
43         header("Content-type: $mimetype");
44         readfile("$pathname");
45     } else {
46         error("Sorry, but the file you are looking for was not found", "course/view.php?id=$courseid");
47     }
49     exit;
50 ?>