You are here

function filefield_file_download in FileField 5.2

Same name and namespace in other branches
  1. 6.3 filefield.module \filefield_file_download()
  2. 6.2 filefield.module \filefield_file_download()

File

./filefield.module, line 809
Defines a file field type.

Code

function filefield_file_download($file) {
  $file = file_create_path($file);
  $result = db_query("SELECT * FROM {files} WHERE filepath = '%s'", $file);
  if (!($file = db_fetch_object($result))) {

    // We don't really care about this file.
    return;
  }
  $node = node_load($file->nid);
  if (!node_access('view', $node)) {

    // You don't have permission to view the node
    // this file is attached to.
    return -1;
  }

  // @todo: check the node for this file to be referenced in a field
  // to determine if it is managed by filefield. and do the access denied part here.
  if (!user_access('view filefield uploads')) {

    // sorry you do not have the proper permissions to view
    // filefield uploads.
    return -1;
  }

  // Well I guess you can see this file.
  $name = mime_header_encode($file->filename);
  $type = mime_header_encode($file->filemime);

  // Serve images and text inline for the browser to display rather than download.
  $disposition = ereg('^(text/|image/)', $file->filemime) || ereg('flash$', $file->filemime) ? 'inline' : 'attachment';
  return array(
    'Content-Type: ' . $type . '; name=' . $name,
    'Content-Length: ' . $file->filesize,
    'Content-Disposition: ' . $disposition . '; filename=' . $name,
    'Cache-Control: private',
  );
}