file_get_content_headers

7 file.inc file_get_content_headers($file)
8 file.inc file_get_content_headers($file)

Examines a file object and returns appropriate content headers for download.

Parameters

$file: A file object.

Return value

An associative array of headers, as expected by file_transfer().

Related topics

1 call to file_get_content_headers()

File

includes/file.inc, line 2478
API for handling file uploads and server file management.

Code

function file_get_content_headers($file) {
  $name = mime_header_encode($file->filename);
  $type = mime_header_encode($file->filemime);
  // Serve images, text, and flash content for display rather than download.
  $inline_types = variable_get('file_inline_types', array('^text/', '^image/', 'flash$'));
  $disposition = 'attachment';
  foreach ($inline_types as $inline_type) {
    // Exclamation marks are used as delimiters to avoid escaping slashes.
    if (preg_match('!' . $inline_type . '!', $file->filemime)) {
      $disposition = 'inline';
    }
  }

  return array(
    'Content-Type' => $type . '; name="' . $name . '"', 
    'Content-Length' => $file->filesize, 
    'Content-Disposition' => $disposition . '; filename="' . $name . '"', 
    'Cache-Control' => 'private',
  );
}

Comments

Only for "Private" upload destination

This function only works with the Private upload destination. There's more overhead with Private uploads vs. Public, but this function is very useful so that users' browsers don't end up opening up files that really should be downloaded.

To enable Private uploads you need to specify a path that is outside your web root:
Configuration > Media > File System > Private file system path

Login or register to post comments