public function sendfile($path, $cb, $pri = EIO_PRI_DEFAULT) { $req = $this; try { $this->header('Content-Type: ' . MIME::get($path)); } catch (RequestHeadersAlreadySent $e) { } if ($this->conn->sendfileCap) { $req->ensureSentHeaders(); $req->conn->onWriteOnce(function ($conn) use($req, $path, $cb, $pri) { FS::sendfile($req->conn->fd, $path, $cb, 0, null, $pri); }); return; } $first = true; FS::readfileChunked($path, $cb, function ($file, $chunk) use($req, &$first) { // readed chunk if ($first) { try { $req->header('Content-Length: ' . $file->stat['st_size']); } catch (RequestHeadersAlreadySent $e) { } $first = false; } $req->out($chunk); }); }
function fileMime($fname) { if (function_exists('pathinfo')) { if ($ext = pathinfo($fname, PATHINFO_EXTENSION)) { $ext = strtolower($ext); $force = MIME::get()->getData(); if (isset($force[".{$ext}"])) { return $force[".{$ext}"]; } } } if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime = finfo_file($finfo, $fname); finfo_close($finfo); return $mime; } return null; }
/** * Sends a file download, invoking the browser's "Save As..." dialog. * * Exits after sending. Unlike the HTTP extension version, this function * also sends Content-Type, Content-Disposition, and "no-cache" headers. * * @param string $file Filepath to file to send. * @param string $filetype File type to send as, default is * 'application/octet-stream'. * @param string $filename Optional name to show to user - defaults to * basename($file). * @return void * @throws RuntimeException if headers already sent, or file is unreadable. */ public function sendFile($file, $filetype = 'download', $filename = null) { if (headers_sent($_file, $_line)) { throw new RuntimeException("Cannot send file - output started in '{$_file}' on line '{$_line}'."); } if (!file_exists($file) || !is_readable($file)) { throw new RuntimeException("Cannot send unknown or unreadable file {$file}."); } if (!isset($filename)) { $filename = basename($file); } header('Expires: 0'); header('Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); $this->sendContentType(MIME::get(strtolower($filetype), 'application/octet-stream')); $this->sendContentDisposition('attachment', $filename); // invalid without Content-Length header('Content-Length: ' . filesize($file)); header('Content-Transfer-Encoding: binary'); header('Connection: close'); readfile($file); exit; }
public function autoindex($dir) { $this->onWakeup(); Daemon::$req = $this; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Index of /</title> <style type="text/css"> a, a:active {text-decoration: none; color: blue;} a:visited {color: #48468F;} a:hover, a:focus {text-decoration: underline; color: red;} body {background-color: #F5F5F5;} h2 {margin-bottom: 12px;} table {margin-left: 12px;} th, td { font: 90% monospace; text-align: left;} th { font-weight: bold; padding-right: 14px; padding-bottom: 3px;} td {padding-right: 14px;} td.s, th.s {text-align: right;} div.list { background-color: white; border-top: 1px solid #646464; border-bottom: 1px solid #646464; padding-top: 10px; padding-bottom: 14px;} div.foot { font: 90% monospace; color: #787878; padding-top: 4px;} </style> </head> <body> <pre class="header">Welcome!</pre><h2>Index of /</h2> <div class="list"> <table summary="Directory Listing" cellpadding="0" cellspacing="0"> <thead> <tr> <th class="n">Name</th> <th class="t">Type</th> </tr> </thead> <tbody> <tr> <td class="n"><a href="../">Parent Directory</a>/</td> <td class="t">Directory</td> </tr> <?php foreach ($dir['dents'] as $item) { $type = $item['type'] === EIO_DT_DIR ? 'Directory' : MIME::get($path); ?> <tr> <td class="n"><a href="<?php echo htmlspecialchars($item['name']) . ($type == 'Directory' ? '/' : ''); ?> "><?php echo htmlspecialchars($item['name']); ?> </a></td> <td class="t"><?php echo $type; ?> </td> </tr> <?php } ?> </tbody> </table> </div> <?php if ($this->upstream->config->expose->value) { ?> <div class="foot">phpDaemon/<?php echo Daemon::$version; ?> </div><?php } ?> </body> </html><?php }