You are here

function _file_resource_retrieve in Services 6.3

Same name and namespace in other branches
  1. 7.3 resources/file_resource.inc \_file_resource_retrieve()

Get a given file

Parameters

$fid: Number. File ID

Return value

The file

1 call to _file_resource_retrieve()
_node_resource_load_node_files in resources/node_resource.inc
Generates an array of base64 encoded files attached to a node
1 string reference to '_file_resource_retrieve'
_file_resource_definition in resources/file_resource.inc
THERE SHOULD BE NO UPDATE!!! Drupal doesn't allow updating or replacing a file in the files table. If you need to, create a new file and remove the old file.

File

resources/file_resource.inc, line 266
File resource.

Code

function _file_resource_retrieve($fid, $file_contents) {
  if ($file = db_fetch_array(db_query('SELECT * FROM {files} WHERE fid = %d', $fid))) {
    if ($file_contents) {
      $absolute_file_path = getcwd() . '/' . file_create_path($file['filepath']);
      $binaryfile = fopen($absolute_file_path, 'rb');
      if ($binaryfile === FALSE) {
        services_error(t('Cannot open file with ID %fid.', array(
          '%fid' => $fid,
        )));
      }
      $file['file'] = base64_encode(fread($binaryfile, filesize($file['filepath'])));
      fclose($binaryfile);
    }
    $file['uri'] = services_resource_uri(array(
      'file',
      $file['fid'],
    ));
    return $file;
  }
  return services_error(t('There is no file with the given ID.'));
}