/**
  * Checks whether the target directory exists, creates it if necessary.
  * Checks the directory permissions, changes it if necessary.
  *
  * @param \Es\Http\UploadedFileInterface $file   The value object, that
  *                                               represents uploaded file
  * @param UploadTargetInterface          $target The target, that represents
  *                                               the new file name
  */
 public function __invoke(UploadedFileInterface $file, UploadTargetInterface $target)
 {
     if ($file->getError() > 0) {
         $this->decideOnFailure(static::UPLOADED_FILE_CONTAINS_ERROR);
         return;
     }
     $dir = $this->getTargetDirectory();
     if (!$dir) {
         $this->decideOnFailure(static::TARGET_DIR_NOT_SPECIFIED);
         return;
     }
     set_error_handler(function ($errno, $errstr) {
         throw new RuntimeException($errstr);
     });
     try {
         if (!file_exists($dir) || !is_dir($dir)) {
             mkdir($dir, $this->getDirPermissions(), true);
         }
     } catch (RuntimeException $ex) {
         restore_error_handler();
         $this->errors[static::CREATE_DIRECTORY_FAILED] = $ex->getMessage();
         $this->decideOnFailure(static::CREATE_DIRECTORY_FAILED);
         return;
     }
     restore_error_handler();
     if (!is_readable($dir)) {
         $this->decideOnFailure(static::DIRECTORY_NOT_READABLE);
         return;
     }
     if (!is_writable($dir)) {
         $this->decideOnFailure(static::DIRECTORY_NOT_WRITABLE);
         return;
     }
     $this->decideOnSuccess();
 }
Exemple #2
0
 /**
  * Moves uploaded file to a specific directory.
  *
  * @param \Es\Http\UploadedFileInterface $file   The value object, that
  *                                               represents uploaded file
  * @param UploadTargetInterface          $target The target, that represents
  *                                               the new file name
  */
 public function __invoke(UploadedFileInterface $file, UploadTargetInterface $target)
 {
     if ($file->getError() > 0) {
         $this->decideOnFailure(static::UPLOADED_FILE_CONTAINS_ERROR);
         return;
     }
     $source = $file->getTempName();
     if (!$source) {
         $this->decideOnFailure(static::UPLOADED_FILE_MISSING_TEMPNAME);
         return;
     }
     if (!is_readable($source)) {
         $this->decideOnFailure(static::MISSING_TEMPORARY_FILE);
         return;
     }
     $dir = $this->getTargetDirectory();
     if (!$dir) {
         $this->decideOnFailure(static::TARGET_DIR_NOT_SPECIFIED);
         return;
     }
     $destination = $dir . PHP_DS . $target;
     set_error_handler(function ($errno, $errstr) {
         throw new RuntimeException($errstr);
     });
     try {
         $sapi = PHP_SAPI;
         switch (true) {
             case empty($sapi) || 0 === strpos($sapi, 'cli'):
                 rename($source, $destination);
                 break;
                 // @codeCoverageIgnoreStart
             // @codeCoverageIgnoreStart
             default:
                 move_uploaded_file($source, $destination);
         }
         // @codeCoverageIgnoreEnd
         chmod($destination, $this->filePermissions);
     } catch (RuntimeException $ex) {
         restore_error_handler();
         $this->errors[static::MOVEMENT_FAILED] = $ex->getMessage();
         $this->decideOnFailure(static::MOVEMENT_FAILED);
         return;
     }
     restore_error_handler();
     $this->decideOnSuccess();
 }