Example #1
0
 public function saveUploadedFile(File &$fileObject)
 {
     // file: name of form input field for file
     // tmp_name: The temporary filename of the file in which the uploaded file was stored on the server.
     if (!empty($_FILES['file']['error'])) {
         switch ($_FILES['file']['error']) {
             case UPLOAD_ERR_FORM_SIZE:
                 throw new FileNotSavedException('The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form');
                 break;
             case UPLOAD_ERR_PARTIAL:
                 throw new FileNotSavedException('The uploaded file was only partially uploaded');
                 break;
             case UPLOAD_ERR_NO_FILE:
                 throw new FileNotSavedException('No file was uploaded');
                 break;
             case UPLOAD_ERR_NO_TMP_DIR:
                 throw new FileNotSavedException('Missing a temporary folder');
                 break;
             case UPLOAD_ERR_CANT_WRITE:
                 throw new FileNotSavedException('Failed to write file to disk');
                 break;
             case UPLOAD_ERR_EXTENSION:
                 throw new FileNotSavedException('A PHP extension stopped the file upload');
                 break;
             default:
                 throw new FileNotSavedException('An undefined error occurred');
                 break;
         }
     }
     if ($_FILES['file']['size'] >= FileUtils::getUploadMaxFileSize()) {
         throw new FileNotSavedException(sprintf('Your uploaded file exceeds the maximum allowed file size of %s', FileUtils::getUploadMaxFileSize()));
     }
     $fileObject->setSize($_FILES['file']['size']);
     $fileExtension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
     if ($fileExtension === null) {
         // no file extension provided
         throw new FileNotSavedException(sprintf('Your uploaded file must have a file extension'));
     }
     $fileObject->setExtension(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
     if (!$this->formatHasType('image', $fileExtension) && !$this->formatHasType('documents', $fileExtension) && !$this->formatHasType('audio', $fileExtension) && !$this->formatHasType('video', $fileExtension)) {
         throw new FileNotSavedException(sprintf('Your uploaded file does not have a valid file extension'));
     }
     // strip whitespaces & german umlauts from filename
     $fileName = explode(sprintf('.%s', $fileExtension), $_FILES['file']['name']);
     if ($fileName === false || is_array($fileName) && empty($fileName) || is_array($fileName) && $fileName[0] === $_FILES['file']['name']) {
         throw new FileNotSavedException('Could not replace whitespaces in filename');
     }
     $fileName = FileUtils::replaceWhitespacesFromString($fileName[0]);
     $_FILES['file']['name'] = $fileName;
     $fileObject->setName($fileName);
     if (file_exists(sprintf('%s/%s.%s', $this->localPathToMediaDir, $_FILES['file']['name'], $fileExtension))) {
         throw new FileNotSavedException('A file with the same name already exists');
     }
     // all checks passed -> move file to media dir
     $code = move_uploaded_file($_FILES['file']['tmp_name'], sprintf('%s/%s.%s', $this->localPathToMediaDir, $_FILES['file']['name'], $fileExtension));
     if ($code === false) {
         // not a valid uploaded file or valid, but cannot be moved
         throw new FileNotSavedException('An unknown error occured');
     }
     $fileObject->setLink(sprintf('%s/%s.%s', $this->httpPathToMediaDir, $_FILES['file']['name'], $fileExtension));
     $fileObject->setLocalPath(sprintf('%s/%s.%s', $this->localPathToMediaDir, $_FILES['file']['name'], $fileExtension));
     // create thumbnail with width of 40px
     ThumbnailHandler::createThumbnail($fileObject, $this->httpPathToMediaDir, $this->localPathToMediaDir, $_FILES['file']['name'], $fileExtension, 40, null);
 }