/** * Makes an array of the content in a dir * @param string $dir Directory to be listed * @return array */ function getDirList($dir) { $dir = deutf8($dir); $dirList = array(); $dir = strpos($dir, '..') !== false ? false : glob($dir); if (is_array($dir)) { $dir = @$dir[0]; } if ($dir && is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $fileType = filetype($dir . $file); if ($fileType == "dir" && !in_array($file, $this->ignore) && $file[0] != '.') { $dirList[$file . "?" . $fileType] = array($fileType, $file, filesize($dir . $file), filemtime($dir . $file)); } elseif ($fileType == "file" && !in_array($file, $this->ignore) && $file[0] != '.') { $dirList[$file . "?" . $fileType] = array($fileType, $file, filesize($dir . $file), filemtime($dir . $file)); } } closedir($dh); } } else { die('Error: ' . $dir . ': Not a directory'); } return $this->sortDirList($dirList); }
function stream($path = false, $force_download = false) { if (!$path) { $path = deutf8($this->path); } $mime = getMime($path); while (ob_get_level() > 0) { ob_end_clean(); } //ob_start('contentSize'); //ob_start('ob_gzhandler'); $last_modified_time = filemtime($path); $etag = md5_file($path); $length = $filesize = filesize($path); $offset = 0; if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time || @trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { header('HTTP/1.1 304 Not Modified'); exit; } if (isset($_SERVER['HTTP_RANGE'])) { // if the HTTP_RANGE header is set we're dealing with partial content // Only supports a single range right now. // http://stackoverflow.com/questions/157318/resumable-downloads-when-using-php-to-send-the-file preg_match('/bytes=(\\d+)-(\\d+)?/', $_SERVER['HTTP_RANGE'], $range); $offset = intval($range[1]); $end = @intval($range[2]) > 0 ? intval($range[2]) : $filesize; if ($offset > $filesize - 1 || $end > $filesize || $offset > $end) { header('HTTP/1.1 416 Requested Range Not Satisfiable'); header('Content-Range: bytes */' . filelength); // Required in 416. exit; } $length = $end - $offset; header('HTTP/1.1 206 Partial Content'); header('Content-Range: bytes ' . $offset . '-' . $end . '/' . $filesize); } header('Etag: "' . $etag . '"'); header('Content-Length: ' . $length); header('Content-Disposition: ' . ($_REQUEST['action'] === 'download' ? 'attachment; ' : '') . 'filename="' . pathinfo($path, PATHINFO_BASENAME) . '"'); header('Content-Transfer-Encoding: binary'); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $last_modified_time) . ' GMT'); header('Content-Type: ' . $mime); if ($_REQUEST['action'] == 'download' || $force_download) { header('Content-Type: application/force-download', false); header('Content-Type: application/download', false); header('Content-Description: File Transfer'); } header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 2 * 24 * 3600) . ' GMT'); header('Cache-Control: private, max-age=172801, must-revalidate'); header('Cache-Control: no-cache'); header('Pragma: cache'); $fp = fopen($path, 'rb'); if ($fp === false) { die; } // error!! FIXME fseek($fp, $offset); print fread($fp, $length); fclose($fp); exit; }