You are here

public function RestfulFilesUpload::createEntity in RESTful 7

Create and save files.

Return value

array Array with a list of file IDs that were created and saved.

Throws

\Exception

Overrides RestfulEntityBase::createEntity

File

plugins/restful/RestfulFilesUpload.php, line 57
Contains RestfulFilesUpload.

Class

RestfulFilesUpload
@file Contains RestfulFilesUpload.

Code

public function createEntity() {
  if (!$_FILES) {
    throw new \RestfulBadRequestException('No files sent with the request.');
  }
  $ids = array();
  foreach ($_FILES as $file_info) {

    // Populate the $_FILES the way file_save_upload() expects.
    $name = $file_info['name'];
    foreach ($file_info as $key => $value) {
      $_FILES['files'][$key][$name] = $value;
    }
    $file = $this
      ->fileSaveUpload($name);

    // Change the file status from temporary to permanent.
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);

    // Required to be able to reference this file.
    file_usage_add($file, 'restful', 'files', $file->fid);
    $ids[] = $file->fid;
  }
  $return = array();
  foreach ($ids as $id) {
    $return[] = $this
      ->viewEntity($id);
  }
  return $return;
}