Typo3: How to upload a file and create a file reference?
Asked Answered
V

2

3

i'll try to upload a file (or later multiple files) in FE. This works, like my current code. But how can i get a file reference of this file now?

/**
 *
 * @var array $fileData
 * @var integer $feUserId
 * @return \TYPO3\CMS\Extbase\Domain\Model\FileReference
 */
private function uploadFile($fileData, $feUserId) {
    $storageRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\StorageRepository');
    $storage           = $storageRepository->findByUid(1); # Fileadmin = 1
    $saveFolder        = $storage->getFolder($this->settings['uploadFolder']);

    // Datei speichern
    $fileObject = $storage->addFile($fileData['tmp_name'], $saveFolder, $feUserId.'_'.$fileData['name']);

    // Dateiobjekt
    $repositoryFileObject = $storage->getFile($fileObject->getIdentifier());

    die(\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($repositoryFileObject));
    #$newFileReference = $this->objectManager->get('TYPO3\CMS\Extbase\Domain\Model\FileReference');
    #$newFileReference->setOriginalResource($repositoryFileObject);

    return $newFileReference;
}
Vidar answered 29/7, 2014 at 18:1 Comment(0)
B
2

There should be something like »setFileReference« by now, but I can not find the like: https://github.com/TYPO3/typo3/blob/TYPO3_6-2/typo3/sysext/core/Classes/Resource/FileReference.php

Well, you may wanna use the following script as temporary solution, which uses the datamap process to create file references.

$sys_file_uid = $file->getUid();
$tt_content_uid = 42;
$tt_content_pid = 1337;

// Do not directly insert a record into sys_file_reference, as this bypasses all sanity checks and automatic updates done!
$data = array();
$data['sys_file_reference']['NEW' . $sys_file_uid] = array(
    'uid_local' => $sys_file_uid,
    'table_local' => 'sys_file',
    'uid_foreign' => $tt_content_uid,
    'tablenames' => 'tt_content',
    'fieldname' => 'image',
    'pid' => $tt_content_pid,
);
$data['tt_content'][$tt_content_uid] = array('image' => 'NEW' . $sys_file_uid);

$tce = t3lib_div::makeInstance('t3lib_TCEmain'); // create TCE instance
$tce->start($data, array());
$tce->process_datamap();
if ($tce->errorLog) {
    // Error - Reference not created
    // t3lib_utility_Debug::viewArray($tce->errorLog);
}
else {
    // Success - Reference created
}
Borlow answered 31/7, 2014 at 9:9 Comment(2)
Beware, it will only work on the Backend and most probably fail on the Frontend as object "t3lib_TCEmain" (in the meantime renamed \TYPO3\CMS\Core\DataHandling\DataHandler) is tight to the BE.Senescent
Is there any way to get all files from a storage repository.Noiseless
P
0

after hitting google for a while i figured out a article that sounded quite well to me.. gonna examine it tomorrow: http://insight.helhum.io/post/85015526410/file-upload-using-extbase-and-fal-in-typo3-6-2

(just in case somebody else needs this before i could test it)

Pollination answered 8/1, 2015 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.