Prado\Web\THttpResponse::writeFile PHP Метод

writeFile() публичный Метод

Make sure not to output anything else after calling this method.
public writeFile ( $fileName, $content = null, $mimeType = null, $headers = null, $forceDownload = true, $clientFileName = null, $fileSize = null )
    public function writeFile($fileName, $content = null, $mimeType = null, $headers = null, $forceDownload = true, $clientFileName = null, $fileSize = null)
    {
        static $defaultMimeTypes = array('css' => 'text/css', 'gif' => 'image/gif', 'png' => 'image/png', 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'htm' => 'text/html', 'html' => 'text/html', 'js' => 'javascript/js', 'pdf' => 'application/pdf', 'xls' => 'application/vnd.ms-excel');
        if ($mimeType === null) {
            $mimeType = 'text/plain';
            if (function_exists('mime_content_type')) {
                $mimeType = mime_content_type($fileName);
            } else {
                if (($ext = strrchr($fileName, '.')) !== false) {
                    $ext = substr($ext, 1);
                    if (isset($defaultMimeTypes[$ext])) {
                        $mimeType = $defaultMimeTypes[$ext];
                    }
                }
            }
        }
        if ($clientFileName === null) {
            $clientFileName = basename($fileName);
        } else {
            $clientFileName = basename($clientFileName);
        }
        if ($fileSize === null || $fileSize < 0) {
            $fileSize = $content === null ? filesize($fileName) : strlen($content);
        }
        $this->sendHttpHeader();
        if (is_array($headers)) {
            foreach ($headers as $h) {
                header($h);
            }
        } else {
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Type: {$mimeType}");
            $this->_contentTypeHeaderSent = true;
        }
        header('Content-Length: ' . $fileSize);
        header("Content-Disposition: " . ($forceDownload ? 'attachment' : 'inline') . "; filename=\"{$clientFileName}\"");
        header('Content-Transfer-Encoding: binary');
        if ($content === null) {
            readfile($fileName);
        } else {
            echo $content;
        }
    }

Usage Example

Пример #1
0
 public function testWriteFile()
 {
     // Don't know how to test headers :(( ...
     throw new PHPUnit_Framework_IncompleteTestError();
     $response = new THttpResponse();
     $response->setBufferOutput(true);
     // Suppress warning with headers
     $response->writeFile(dirname(__FILE__) . '/data/aTarFile.md5', null, 'text/plain', array('Pragma: public', 'Expires: 0'));
     self::assertContains('4b1ecb0b243918a8bbfbb4515937be98  aTarFile.tar', ob_get_clean());
 }