Esempio n. 1
0
 /**
  * Return the file contents.
  *
  * @param string $fileName
  * @throws \Exception
  * @return array(string, string) [$contentType, $content]
  */
 public function readFile($fileName)
 {
     $this->validateConnectionSettings();
     /** @var LocalFileConnectionSettings $connectionSettings */
     $connectionSettings = $this->getConnectionSettings();
     $rootPath = $connectionSettings->getRootPath();
     $normalizedPath = FileUtils::normalizePath($fileName, true);
     $localFileName = $rootPath . DIRECTORY_SEPARATOR . $normalizedPath;
     if (!file_exists($localFileName)) {
         throw new \Exception($this->getFileNotFoundMessage($fileName));
     }
     $content = file_get_contents($fileName);
     $contentType = FileUtils::getContentTypeFromFileName($fileName);
     return array($contentType, $content);
 }
Esempio n. 2
0
 /**
  * Serve the specified file to standard output.
  *
  * @param string $file The file to be served.
  * @param boolean $output If true, error messages will be printed.
  * @throws \Exception
  * @return boolean
  */
 public function serveFile($file, $output = true)
 {
     /** @var LocalFileConnectionSettings $connectionSettings */
     $connectionSettings = $this->getConnectionSettings();
     if (!$connectionSettings instanceof LocalFileConnectionSettings) {
         throw new \Exception('Invalid connection settings.');
     }
     $connectionSettings->validate();
     $normalizedFilePath = FileUtils::normalizePath($file, true);
     $filePath = $connectionSettings->getRootPath() . DIRECTORY_SEPARATOR . $normalizedFilePath;
     if (!file_exists($filePath)) {
         $this->setHttpResponseCode(404, $output);
         if ($output) {
             printf('<p>The file <strong>%s</strong> could not be found.</p>', $normalizedFilePath);
         }
         return false;
     }
     list($fileSize, $contentType) = $this->getFileInfo($filePath);
     $this->writeHeaders($contentType, $fileSize, $filePath);
     readfile($filePath);
     return true;
 }