0

I have an extension where user can upload in frontend some images to their frontend-profile. Image upload and saving to user-profile works fine, I user following code - but is there a simple way to add title, alternative and description too?

Thanks for help! Martin

/** @var \TYPO3\CMS\Core\Resource\StorageRepository $storageRepository */
        $storageRepository = $this->objectManager->get('TYPO3\CMS\Core\Resource\StorageRepository');
        /** @var \TYPO3\CMS\Core\Resource\ResourceStorage $storage */
        $storage = $storageRepository->findByUid('1');

        #$storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
        #$storage           = $storageRepository->findByUid(1); # Fileadmin = 1
        #$saveFolder        = $storage->getFolder($this->settings['uploadFolder']);

        $fileData = array();
        $fileData['name'] = $_FILES['tx_key_datenwartung']['name']['logo'][0];
        $fileData['type'] = $_FILES['tx_key_datenwartung']['type']['logo'][0];
        $fileData['tmp_name'] = $_FILES['tx_key_datenwartung']['tmp_name']['logo'][0];
        $fileData['size'] = $_FILES['tx_key_datenwartung']['size']['logo'][0];


        // this will already handle the moving of the file to the storage:
        $newFileObject = $storage->addFile(
                $fileData['tmp_name'], $saveFolder, $userUid.'_'.$fileData['name']
        );
        $newFileObject = $storage->getFile($newFileObject->getIdentifier());
        #$newFile = $this->fileRepository->findByUid($newFileObject->getProperty('uid'));

        $newFile = $newFileObject->getUid();

        $persistenceManager = $this->objectManager->get('TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager');
        $persistenceManager->persistAll();

        /** @var \Vendor\Myextension\Domain\Model\FileReference $newFileReference */
        $newFileReference = $this->objectManager->get('Ven\Key\Domain\Model\FileReference');
        $newFileReference->setFile($newFile);


        $user->addLogo($newFileReference);
Daniel
  • 6,916
  • 2
  • 36
  • 47
matin
  • 303
  • 2
  • 13
  • 1
    **Side-note:** `$fileData['type']` is user-submitted data. You should validate the file contents concerning it's mime-type and check if it's allowed at all. Otherwise executable files (PHP, Perl, whatever) could be uploaded by your users to hijack the system. – Oliver Hader Nov 20 '16 at 14:47

1 Answers1

0

.... could solve it myself. just add setter to model and add texts:

/**
 * Set alternative
 *
 * @param \xx\yy\Domain\Model\File $file
 */
public function setAlternative($alternative) {
    $this->alternative = $alternative;
}
and to controller:

$newFileReference->setAlternative('your alternative text for image');

matin
  • 303
  • 2
  • 13