Example #1
0
 /**
  * {@inheritDoc}
  */
 public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false, $object)
 {
     if (null === $fileInfo->isUploadedFile()) {
         $file = $fileInfo->getTmpName();
         $path = str_replace(DIRECTORY_SEPARATOR, '/', $this->getRelativePath($file));
     }
     $info = parent::moveFile($fileInfo, $path, $filenameGeneratorClass, $overwrite, $appendNumber, $object);
     if (null === $fileInfo->isUploadedFile()) {
         $info['origFileName'] = $fileInfo->getOrigName();
     }
     return $info;
 }
 /**
  * Moves the file to the specified path
  *
  * @param FileInfo\FileInfoInterface $fileInfo
  * @param $path
  * @param bool $filenameGeneratorClass
  * @param bool $overwrite
  * @param bool $appendNumber
  * @return array
  * @throws \Gedmo\Exception\UploadableUploadException
  * @throws \Gedmo\Exception\UploadableNoFileException
  * @throws \Gedmo\Exception\UploadableExtensionException
  * @throws \Gedmo\Exception\UploadableIniSizeException
  * @throws \Gedmo\Exception\UploadableFormSizeException
  * @throws \Gedmo\Exception\UploadableFileAlreadyExistsException
  * @throws \Gedmo\Exception\UploadablePartialException
  * @throws \Gedmo\Exception\UploadableNoTmpDirException
  * @throws \Gedmo\Exception\UploadableCantWriteException
  */
 public function moveFile(FileInfoInterface $fileInfo, $path, $filenameGeneratorClass = false, $overwrite = false, $appendNumber = false)
 {
     if ($fileInfo->getError() > 0) {
         switch ($fileInfo->getError()) {
             case 1:
                 $msg = 'Size of uploaded file "%s" exceeds limit imposed by directive "upload_max_filesize" in php.ini';
                 throw new UploadableIniSizeException(sprintf($msg, $fileInfo->getName()));
             case 2:
                 $msg = 'Size of uploaded file "%s" exceeds limit imposed by option MAX_FILE_SIZE in your form.';
                 throw new UploadableFormSizeException(sprintf($msg, $fileInfo->getName()));
             case 3:
                 $msg = 'File "%s" was partially uploaded.';
                 throw new UploadablePartialException(sprintf($msg, $fileInfo->getName()));
             case 4:
                 $msg = 'No file was uploaded!';
                 throw new UploadableNoFileException(sprintf($msg, $fileInfo->getName()));
             case 6:
                 $msg = 'Upload failed. Temp dir is missing.';
                 throw new UploadableNoTmpDirException($msg);
             case 7:
                 $msg = 'File "%s" couldn\'t be uploaded because directory is not writable.';
                 throw new UploadableCantWriteException(sprintf($msg, $fileInfo->getName()));
             case 8:
                 $msg = 'A PHP Extension stopped the uploaded for some reason.';
                 throw new UploadableExtensionException(sprintf($msg, $fileInfo->getName()));
             default:
                 throw new UploadableUploadException(sprintf('There was an unknown problem while uploading file "%s"', $fileInfo->getName()));
         }
     }
     $info = array('fileName' => '', 'fileExtension' => '', 'fileWithoutExt' => '', 'filePath' => '', 'fileMimeType' => $fileInfo->getType(), 'fileSize' => $fileInfo->getSize());
     $info['fileName'] = basename($fileInfo->getName());
     $info['filePath'] = $path . '/' . $info['fileName'];
     $hasExtension = strrpos($info['fileName'], '.');
     if ($hasExtension) {
         $info['fileExtension'] = substr($info['filePath'], strrpos($info['filePath'], '.'));
         $info['fileWithoutExt'] = substr($info['filePath'], 0, strrpos($info['filePath'], '.'));
     } else {
         $info['fileWithoutExt'] = $info['fileName'];
     }
     // Now we generate the filename using the configured class
     if ($filenameGeneratorClass) {
         $filename = $filenameGeneratorClass::generate(str_replace($path . '/', '', $info['fileWithoutExt']), $info['fileExtension']);
         $info['filePath'] = str_replace($info['fileName'], $filename, $info['filePath']);
         $info['fileName'] = $filename;
     }
     if (is_file($info['filePath'])) {
         if ($overwrite) {
             $this->removeFile($info['filePath']);
         } else {
             if ($appendNumber) {
                 $counter = 1;
                 $info['filePath'] = $info['fileWithoutExt'] . '-' . $counter . $info['fileExtension'];
                 do {
                     $info['filePath'] = $info['fileWithoutExt'] . '-' . ++$counter . $info['fileExtension'];
                 } while (is_file($info['filePath']));
             } else {
                 throw new UploadableFileAlreadyExistsException(sprintf('File "%s" already exists!', $info['filePath']));
             }
         }
     }
     if (!$this->doMoveFile($fileInfo->getTmpName(), $info['filePath'], $fileInfo->isUploadedFile())) {
         throw new UploadableUploadException(sprintf('File "%s" was not uploaded, or there was a problem moving it to the location "%s".', $fileInfo->getName(), $path));
     }
     return $info;
 }