/** * Populate object from file * * @param string $file * @param string $mime * @param bool|string $attachment * @param bool $unlink * @return \Zork\Http\PhpEnvironment\Response\Readfile * @throws \Zend\Http\Exception\InvalidArgumentException */ public static function fromFile($file, $mime = null, $attachment = false, $unlink = false) { if (!is_file($file) || filesize($file) < 1) { throw new Exception\InvalidArgumentException('$file must be a valid file with size at least 1 byte'); } $readfile = new static($file, $unlink); if (null === $mime && class_exists('finfo', false)) { $finfo = @finfo_open(defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME); if (!empty($finfo)) { $mime = finfo_file($finfo, $file); } } if (null !== $mime) { $readfile->getHeaders()->addHeaderLine('Content-Type', $mime); } if ($attachment) { if (!is_string($attachment) || $attachment === '1') { $attachment = $file; } $filename = basename($attachment); // @codeCoverageIgnoreStart if (function_exists('iconv')) { $filename = @iconv('UTF-8', 'ASCII//TRANSLIT', $filename); } else { $filename = @mb_convert_encoding($filename, 'ASCII', 'auto'); } // @codeCoverageIgnoreEnd if (!empty($filename)) { $readfile->getHeaders()->addHeaderLine('Content-Disposition', 'attachment; filename="' . str_replace('"', '\\"', $filename) . '"'); } } return $readfile; }
/** * Populate object from file-data * * @param \Zork\Data\FileInterface $file * @param bool|string $attachment * @return \Zork\Http\PhpEnvironment\Response\Readfile * @throws \Zend\Http\Exception\InvalidArgumentException */ public static function fromData(FileInterface $file, $attachment = false) { $filedata = new static($file); $mime = $file->getMimeType(); if (null !== $mime) { $filedata->getHeaders()->addHeaderLine('Content-Type', $mime); } if ($attachment) { $attachment = (string) $attachment; $filename = basename($attachment); // @codeCoverageIgnoreStart if (function_exists('iconv')) { $filename = @iconv('UTF-8', 'ASCII//TRANSLIT', $filename); } else { $filename = @mb_convert_encoding($filename, 'ASCII', 'auto'); } // @codeCoverageIgnoreEnd if (!empty($filename)) { $filedata->getHeaders()->addHeaderLine('Content-Disposition', 'attachment; filename="' . str_replace('"', '\\"', $filename) . '"'); } } return $filedata; }
/** * @param ServerRequest $request * @return mixed */ public static function get(ServerRequest $request) { $reader = new static($request); return $reader->getHeaders(); }