/** * Validates a single uploaded file * * @param $aFile UploadedFile * * @throws UploadException on validation exception */ private function validateUpload(UploadedFile $aFile) { $uploadDir = $this->options->getUploadDir(); if ($uploadDir && !$uploadDir->isDir()) { throw new UploadException("Upload dir does not exist"); } $maxSize = $this->options->getMaxSize(); $mimeTypes = $this->options->getMimetypes(); $errors = array(); // PHP system errors if ($aFile->getError()) { switch ($aFile->getError()) { case UPLOAD_ERR_CANT_WRITE: $errors[] = 'Failed to write file to disk.'; break; case UPLOAD_ERR_EXTENSION: $errors[] = 'A PHP extension stopped the file upload.'; break; case UPLOAD_ERR_FORM_SIZE: $errors[] = $aFile->getOriginalFilename() . " exceeds max size. Allowed is " . $this->formatSize($maxSize); break; case UPLOAD_ERR_INI_SIZE: $errors[] = $aFile->getOriginalFilename() . " exceeds max size. Allowed is " . $this->formatSize($maxSize); break; case UPLOAD_ERR_NO_FILE: $errors[] = 'No file was uploaded.'; break; case UPLOAD_ERR_NO_TMP_DIR: throw new UploadException('Missing a temporary folder.'); break; case UPLOAD_ERR_PARTIAL: $errors[] = 'The uploaded file was only partially uploaded. '; break; } } else { if ($maxSize !== null) { if ($aFile->getSize() > $maxSize) { $errors[] = $aFile->getOriginalFilename() . " exceeds max size. Allowed is " . $this->formatSize($maxSize) . " but was " . $this->formatSize($aFile->getSize()); } } if ($mimeTypes !== null && count($mimeTypes) > 0) { if (!in_array($aFile->getMimeType(), $mimeTypes)) { $errors[] = $aFile->getOriginalFilename() . " is not allowed to be uploaded (" . $aFile->getMimeType() . ")"; } } // Upload dir can be null if ($this->options->getAllowOverwrite() === false && $uploadDir && $aFile->existsInDir($uploadDir)) { $errors[] = "File " . $aFile->getOriginalFilename() . " already exists."; } } // if there are errors, throw an exception if (count($errors) > 0) { $excString = ""; foreach ($errors as $error) { $excString .= $error . "\n"; } throw new UploadException(trim($excString)); } }
public static function createFrom(UploadedFile $aOriginal, \SplFileInfo $aFile) { return new UploadedFile($aOriginal->getUploadArrayWithNewPath($aFile->getPathname())); }