You are here

protected function elFinderDrupal::file in elFinder file manager 8.2

Required to output file in browser when volume URL is not set Return array contains opened file pointer, root itself and required headers

@author Dmitry (dio) Levashov

Parameters

array command arguments:

Return value

array

File

src/Controller/elFinderDrupal.php, line 176
elFinder conenctor class

Class

elFinderDrupal
@file

Namespace

Drupal\elfinder\Controller

Code

protected function file($args) {
  $target = $args['target'];
  $download = !empty($args['download']);
  $h403 = 'HTTP/1.x 403 Access Denied';
  $h404 = 'HTTP/1.x 404 Not Found';
  if (($volume = $this
    ->volume($target)) == FALSE) {
    return array(
      'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
      'header' => $h404,
      'raw' => TRUE,
    );
  }
  if (($file = $volume
    ->file($target)) == FALSE) {
    return array(
      'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
      'header' => $h404,
      'raw' => TRUE,
    );
  }
  if (!$file['read']) {
    return array(
      'error' => self::$errors[self::ERROR_ACCESS_DENIED],
      'header' => $h403,
      'raw' => TRUE,
    );
  }
  if ($volume
    ->driverId() != 'f' && ($fp = $volume
    ->open($target)) == FALSE) {
    return array(
      'error' => self::$errors[self::ERROR_FILE_NOT_FOUND],
      'header' => $h404,
      'raw' => TRUE,
    );
  }
  $mime = $download ? 'application/octet-stream' : $file['mime'];
  $result = array(
    'volume' => $volume,
    'pointer' => $fp,
    'info' => $file,
    'header' => array(
      "Content-Type: " . $mime,
      "Content-Disposition: " . $this
        ->GetContentDisposition($file['name'], $mime, $download),
      "Content-Location: " . $file['name'],
      'Content-Transfer-Encoding: binary',
      "Content-Length: " . $file['size'],
      "Connection: close",
    ),
  );
  $real_path = $this
    ->realpath($target);
  module_invoke_all('file_download', $volume
    ->drupalpathtouri($real_path));
  return $result;
}