You are here

function file_service_get_node_files in Services 7

Same name and namespace in other branches
  1. 6 services/file_service/file_service.inc \file_service_get_node_files()
  2. 6.2 services/file_service/file_service.inc \file_service_get_node_files()

Generates an array of base64 encoded files attached to a node

Parameters

$nid: Number. Node ID

Return value

Array. A list of all files from the given node

1 string reference to 'file_service_get_node_files'
file_service_service in services/file_service/file_service.module
Implementation of hook_service().

File

services/file_service/file_service.inc, line 146
@author Services Dev Team

Code

function file_service_get_node_files($nid) {
  $node = node_load($nid);
  if (isset($node->files)) {
    $files = array();
    foreach ($node->files as $file) {

      // Rebuild the files array so it only contains files we know we're allowed to list
      if ($file->list) {
        $files[] = $file;
      }
    }
    if (count($files) > 0) {
      $send = array();
      foreach ($files as $file) {
        $file = array_shift($files);
        $filepath = file_create_path($file->filepath);
        $binaryfile = fopen($filepath, 'rb');
        $send[$file->fid] = array(
          'file' => base64_encode(fread($binaryfile, filesize($filepath))),
          'filename' => $file->filename,
          'uid' => $file->uid,
          'filemime' => $file->filemime,
          'filesize' => $file->filesize,
          'status' => $file->status,
          'timestamp' => $file->timestamp,
        );
        fclose($binaryfile);
      }
    }
    return $send;
  }
  else {
    return services_error(t('There are no files on given node.'));
  }
}