You are here

protected function DataProviderFile::checkUploadErrors in RESTful 7.2

Checks of there is an error with the file upload and throws an exception.

Parameters

string $source: The name of the uploaded file

array $files: Array containing information about the files.

Throws

\Drupal\restful\Exception\BadRequestException

\Drupal\restful\Exception\ServiceUnavailableException

1 call to DataProviderFile::checkUploadErrors()
DataProviderFile::fileSaveUpload in src/Plugin/resource/DataProvider/DataProviderFile.php
An adaptation of file_save_upload() that includes more verbose errors.

File

src/Plugin/resource/DataProvider/DataProviderFile.php, line 275
Contains \Drupal\restful\Plugin\resource\DataProvider\DataProviderFile.

Class

DataProviderFile
Class DataProviderFile.

Namespace

Drupal\restful\Plugin\resource\DataProvider

Code

protected function checkUploadErrors($source, array $files) {

  // Check for file upload errors and return FALSE if a lower level system
  // error occurred. For a complete list of errors:
  // See http://php.net/manual/features.file-upload.errors.php.
  switch ($files['files']['error'][$source]) {
    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
      $message = format_string('The file %file could not be saved, because it exceeds %maxsize, the maximum allowed size for uploads.', array(
        '%file' => $files['files']['name'][$source],
        '%maxsize' => format_size(file_upload_max_size()),
      ));
      throw new BadRequestException($message);
    case UPLOAD_ERR_PARTIAL:
    case UPLOAD_ERR_NO_FILE:
      $message = format_string('The file %file could not be saved, because the upload did not complete.', array(
        '%file' => $files['files']['name'][$source],
      ));
      throw new BadRequestException($message);
    case UPLOAD_ERR_OK:

      // Final check that this is a valid upload, if it isn't, use the
      // default error handler.
      if ($this::isUploadedFile($files['files']['tmp_name'][$source])) {
        break;
      }
    default:

      // Unknown error.
      $message = format_string('The file %file could not be saved. An unknown error has occurred.', array(
        '%file' => $files['files']['name'][$source],
      ));
      throw new ServiceUnavailableException($message);
  }
}